E
E
Eduard Valeev2020-01-18 21:06:26
WooCommerce
Eduard Valeev, 2020-01-18 21:06:26

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

2 answer(s)
E
Eduard Valeev, 2020-01-30
@Adward

Decided like this:

if( is_product_category()  ){
  $category = get_queried_object();
  if( ($category->count) == 0){	//	Если количество товаров равно нулю возвращаем 404 заголовок
    header("HTTP/1.0 404 Not Found");
  }
}

O
Orkhan Hasanli, 2020-01-18
@azerphoenix

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' );

https://www.themelocation.com/how-to-hide-empty-ca...
Google

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question