• Hello, I was wondering if anybody knows of a way to only display the current categories parents(up to the top level) and children in the woocommerce product categories widget?

    So as an example if your category structure looks like this:

    Drums
    –Drum Heads
    –Cymbals
    —High Hats
    —Open Hats
    —Crash
    Guitar
    –Electric
    –Accoustic
    Bass
    Keyboard

    And you were currently displaying the Crash Category it would display everything in the category above it. But it wouldnt display any of the other categories.

    I found this functions snippet, which will only show your current category on down. But I want to show the parents. Does anybody know how this current code can be altered to achive what I want to do, or recommend/point me to another solution?

    Thanks,

    add_filter('woocommerce_product_categories_widget_args','woo_current_product_category');
    function woo_current_product_category( $args ){
    	
    	global $wp_query, $post, $woocommerce;
    	
    	$include = array();
    	
    	$category_parent 	= '';
    	$current_cat		= '';
    	
    	if ( is_tax( 'product_cat' ) ) {
    		
    		$cat_obj = $wp_query->get_queried_object();
    		
    		if( isset( $cat_obj->term_id ) ){
    			
    			$current_cat	 = $cat_obj;			
    			$category_parent = $cat_obj->parent;
    		}
    		
    	} elseif ( is_singular('product') ) {
    		
    		$product_category 	= wc_get_product_terms( $post->ID, 'product_cat', array( 'orderby' => 'parent', 'fields' => 'all' ) );
    		$current_cat 		= end( $product_category );
    		$category_parent 	= $current_cat->parent;
    				
    	}
    	
    	//check if current cat has children
    	if( ! empty( $current_cat ) )
    		$current_cat_children = 
    			get_terms( 
    				'product_cat', 
    				array( 
    					'parent' => $current_cat->term_id, 
    					'fields' => 'ids', 
    					'hide_empty' => 0 
    				) 
    			);
    	
    	if( ! empty( $current_cat_children ) ){
    		
    		$terms_to_include = 
    			array_merge( 
    				array( $current_cat->term_id ), 
    				$current_cat_children 
    			);
    		
    	}else{
    		
    		if( ! empty( $category_parent ) ){
    			$terms_to_include = 
    				array_merge( 
    					array( $category_parent ), 
    					get_terms( 'product_cat', array( 'parent' => $category_parent, 'fields' => 'ids', 'hide_empty' => 0 ) )
    				 );
    		}
    			
    	}
    	
    	if( ! empty( $terms_to_include ) )
    		$include = $terms_to_include;
    		
    	if( ! empty( $include ) )
    		$args['include'] = $include;
    	
    	return $args;
    
    }
    • This topic was modified 7 years, 4 months ago by pierrehooker.
Viewing 1 replies (of 1 total)
  • Thread Starter pierrehooker

    (@pierrehooker)

    So just to clarify my example, if you are in the cymbals category the widget would display:

    Drums
    –Drum Heads
    –Cymbals
    —High Hats
    —Open Hats
    —Crash

Viewing 1 replies (of 1 total)
  • The topic ‘ONLY show current category parent and children in cat widget?’ is closed to new replies.