I am developing a child theme for the Storefront Theme. I use the Product Category Widget as a dropdown under the header which fits my needs perfectly, though I need the same (if possible) dropdown menu to show up on every Category Page, instead of just the Main Page.
I am customizing this code which almost does it:
/**
* WooCommerce Extra Feature
* --------------------------
*
* Register a shortcode that creates a product categories dropdown list
*
* Use: [product_categories_dropdown orderby="title" count="0" hierarchical="0"]
*/
add_shortcode( 'product_categories_dropdown', 'woo_product_categories_dropdown' );
function woo_product_categories_dropdown( $atts ) {
extract( shortcode_atts(array(
'count' => '0',
'hierarchical' => '0',
'orderby' => ''
), $atts ) );
ob_start();
// Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
wc_product_dropdown_categories( array(
'orderby' => ! empty( $orderby ) ? $orderby : 'order',
'hierarchical' => $hierarchical,
'show_uncategorized' => 0,
'show_counts' => $count
) );
?>
<script type='text/javascript'>
/* <![CDATA[ */
jQuery(function(){
var product_cat_dropdown = jQuery(".dropdown_product_cat");
function onProductCatChange() {
if ( product_cat_dropdown.val() !=='' ) {
location.href = "<?php echo esc_url( home_url() ); ?>/?product_cat=" +product_cat_dropdown.val();
}
}
product_cat_dropdown.change( onProductCatChange );
});
/* ]]> */
</script>
<?php
return ob_get_clean();
}
Now I need to hide the counters and show the empty Categories.
I haven't be able to get that.
How can I hide the counters and show the empty Categories?
In your code there was:
'show_count'
(withouts
) … Now hiding counters is enabled and functional.In this shortcode you can alter the following optional arguments:
hierarchical
that is disabled by default (set to '0')hide_empty
that is disabled by default (set to '0')show_count
that is now disabled by default (set to '0')depth
that is disabled by default (set to '0')orderby
set to category "order" by default (can be by names too: "name")Added a custom hook
woocommerce_product_categories_shortcode_dropdown_args
that will allow extended customizations…Here is the new code:
Code goes in function.php file of the active child theme (or active theme).
Tested and works.
1) Example usage - All product categories and subcategories hierarchically displayed:
In php code you can use it this way:
or inserted in html tags:
2) Example usage - Only "main parent" product categories:
In php code you can use it this way:
or inserted in html tags: