Dazzling - Woocommerce [product_categories] shortcode code change

Hi Colorlib team, firstly a heads up… I’ve always had good experience with your support.

Secondly, my brief set up which MAY help:

  • Dazzling theme
  • Have a 'dazzling-child' theme installed. I'm under the impression the child theme is being used, since any custom CSS I've done, takes effect.

Woocommerce - I need to change the coding specifically for the shortcode [product_categories] so that it returns all categories except a few.

I tried copying the below code/function, which I get from the woocommerce/includes folder, to functions.php and even tried copying the whole ‘class-wc-shortcodes.php’ file into the child theme folder using the same folder structure ‘/dazzling-child/woocommerce/includes’. When I do any of the above, it either doesn’t take effect or if I copy/paste into functions.php, it crashes the site.

Any ideas here? I’m not even sure if this involves your theme or woocommerce, or I just don’t understand the structure of things yet.

Looking forward to your reply, thanks.

/**
	 * List all (or limited) product categories
	 *
	 * @param array $atts
	 * @return string
	 */
	public static function product_categories( $atts ) {
		global $woocommerce_loop;

		$atts = shortcode_atts( array(
			'number'     => null,
			'orderby'    => 'name',
			'order'      => 'ASC',
			'columns'    => '4',
			'hide_empty' => 1,
			'parent'     => '',
			'ids'        => ''
		), $atts );

		if ( isset( $atts['ids'] ) ) {
			$ids = explode( ',', $atts['ids'] );
			$ids = array_map( 'trim', $ids );
		} else {
			$ids = array();
		}

		$hide_empty = ( $atts['hide_empty'] == true || $atts['hide_empty'] == 1 ) ? 1 : 0;

		// get terms and workaround WP bug with parents/pad counts
		$args = array(
			'orderby'    => $atts['orderby'],
			'order'      => $atts['order'],
			'hide_empty' => $hide_empty,
			'include'    => $ids,
			'pad_counts' => true,
			'child_of'   => $atts['parent']
		);

		$product_categories = get_terms( 'product_cat', $args );

		if ( '' !== $atts['parent'] ) {
			$product_categories = wp_list_filter( $product_categories, array( 'parent' => $atts['parent'] ) );
		}

		if ( $hide_empty ) {
			foreach ( $product_categories as $key => $category ) {
				if ( $category->count == 0 ) {
					unset( $product_categories[ $key ] );
				}
			}
		}

		if ( $atts['number'] ) {
			$product_categories = array_slice( $product_categories, 0, $atts['number'] );
		}

		$columns = absint( $atts['columns'] );
		$woocommerce_loop['columns'] = $columns;

		ob_start();

		// Reset loop/columns globals when starting a new loop
		$woocommerce_loop['loop'] = $woocommerce_loop['column'] = '';

		if ( $product_categories ) {
			woocommerce_product_loop_start();

			foreach ( $product_categories as $category ) {
				wc_get_template( 'content-product_cat.php', array(
					'category' => $category
				) );
			}

			woocommerce_product_loop_end();
		}

		woocommerce_reset_loop();

		return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
	}

Hi @drazeni,

I hope you are well today and thank you for your question.

This question is not specific to Dazzling theme so i have moved it to other forum.

This question is related to WooCommerce plugin so you can contact the author of this plugin on the following pages to get code related help.

Developing custom code for custom functionality is beyond the scope of support that we provide here.

If you are not a developer then you can consider hiring a developer to develop it for you. You can hire a developer from any freelance site. We recommend the developer https://www.upwork.com/freelancers/~011652ffec8865c6d5

Best Regards,
Movin

Thank you Movin, I appreciate your reply and I understand. I thought to check with you guys first.