问题
I have a few products that are uncategorized, because they do not belong to any specific product category.
I'd like to keep the products in the uncategorized group available on my website (when people search for it, when I show it on the frontpage etc), but I want to hide the actual uncategorized tab from the category dropdown etc, so people do not see it.
I tried this code, but without luck:
// Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_filter( 'woocommerce_product_subcategories_args', 'custom_woocommerce_product_subcategories_args' );
function custom_woocommerce_product_subcategories_args( $args ) {
$args['exclude'] = get_option( 'default_product_cat' );
return $args;
}
Here in this image you can see the issue. It says "Ukategoriseret", which means uncategorized:
回答1:
I suppose that get_option( 'default_product_cat' )
is something custom as when testing that I don't get any output. So you have to be sure that you are getting a term ID with it.
I suppose also that you are talking about the product category widget. If it's the case you will need to use one of this hooks, depending on the display selected option settings.
The code bellow works to exclude any set of product categories term IDs from product category widget:
add_filter('woocommerce_product_categories_widget_dropdown_args', 'widget_product_categories_list_args', 10, 1);
add_filter('woocommerce_product_categories_widget_args', 'widget_product_categories_list_args', 10, 1);
function widget_product_categories_list_args( $args ) {
$default_term_id = get_option( 'default_product_cat' );
// Excluding: a term ID or coma separated term IDs
$args['exclude'] = array( $default_term_id );
return $args;
}
Code goes in function.php file of your active child theme (active theme).
Tested and works.
来源:https://stackoverflow.com/questions/48730862/exclude-a-specific-term-from-product-categories-widget-in-woocommerce