Skip to content
Advertisement

woocommerce add category nav to product-category pages or dynamic pages

I have the product categories navbar on top of the shop page. If I click on any of the category navbar, then it take me to the product-categories page, however, on top of this page, the categories navbar are unavailable. I need to embed this product category navbar to the product-categories pages too. I’m unable to add shortcodes [product_categories] because, the product-categories page is dynamic i.e. based on the filters. I’ve played with customize>woocommerce>shop page display & category display but nothing worked. Any assistance on this is much appreciated. Attached screen shot reference.

enter image description here

enter image description here

Advertisement

Answer

You can use WooCommerce hooks. for example

function filter_woocommerce_catalog_orderby( $array ) { 
    



  $taxonomy     = 'product_cat';
  $orderby      = 'name';  
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 1;      // 1 for yes, 0 for no  
  $title        = '';  
  $empty        = 0;

  $args = array(
         'taxonomy'     => $taxonomy,
         'orderby'      => $orderby,
         'show_count'   => $show_count,
         'pad_counts'   => $pad_counts,
         'hierarchical' => $hierarchical,
         'title_li'     => $title,
         'hide_empty'   => $empty
  );
 $all_categories = get_categories( $args );
 foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;       
        echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';

        $args2 = array(
                'taxonomy'     => $taxonomy,
                'child_of'     => 0,
                'parent'       => $category_id,
                'orderby'      => $orderby,
                'show_count'   => $show_count,
                'pad_counts'   => $pad_counts,
                'hierarchical' => $hierarchical,
                'title_li'     => $title,
                'hide_empty'   => $empty
        );
        $sub_cats = get_categories( $args2 );
        if($sub_cats) {
            foreach($sub_cats as $sub_category) {
                echo  $sub_category->name ;
            }   
        }
    }       
}

    
    return $array; 
}; 
         
// add the filter 
add_filter( 'woocommerce_catalog_orderby', 'filter_woocommerce_catalog_orderby', 10, 1 ); 
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement