Answer the question
In order to leave comments, you need to log in
Woocommerce: How to not display categories where there are no products (or remove categories in draft)?
In the IM of the list of goods removed in the draft, only a small part of the goods is displayed, for which special product categories are allocated. The remaining products, as necessary, are pulled out of the draft, edited and published in new categories.
However, if we can draft goods, then we cannot do this with categories, and they are physically available in the engine as an entity. And also they are available by direct url. Consequently, the search engine indexes a page with a product category, where there is only one inscription - "there are no products in this category."
Is there a hook, or any other possibility, to redirect categories that don't have products to a special 404 page?
Answer the question
In order to leave comments, you need to log in
Decided like this:
if( is_product_category() ){
$category = get_queried_object();
if( ($category->count) == 0){ // Если количество товаров равно нулю возвращаем 404 заголовок
header("HTTP/1.0 404 Not Found");
}
}
Hello!
1) you can block access to categories through robots
2) Depending on how you display categories on the page. If this is a widget, then you can hide it through a hook.
functionwoo_hide_product_categories_widget( $list_args ){
$list_args[ 'hide_empty' ] = 1;
return $list_args;
}
add_filter( 'woocommerce_product_categories_widget_args', 'woo_hide_product_categories_widget' );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question