I want to target all pages (product categories) that are displaying the products themselves.
My woocommerce settings set to show only categories/subcategories (unless there are none) and then by default the products grid is displayed on the page.
is_product_category() function doesn’t help since it also targets the parent categories which don’t have direct products in them.
Most of my categories that don’t any subcategories in them are grandchildren categories if it matters for the answer.
How can I achieve it and to use it in woocommerce hooks?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can use the woocommerce_products_will_display(). This function returns true if the current shop page is going to display products. This will be the case if your shop pages are set to display products or subcategories and products, but it will be false if the shop pages are set to display subcategories only, and the current category has subcategories.
if ( woocommerce_products_will_display() ) {
// Products are showing.
} else {
// Products are not showing.
}
The woocommerce_get_loop_display_mode() is similar, but can let you know whether subcategories are also displaying:
switch ( woocommerce_get_loop_display_mode() ) {
case 'products':
// Products are displaying.
break;
case 'subcategories':
// Subcategories are displaying.
break;
case 'both':
// Products and subcategories are displaying.
break;
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0