0

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?

0

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;
}
2
  • This is awesome and it did help me! Thank you. But if these woo functions didn't exist for example (or might be renamed/deleted in next woo versions) How else would you check if the current displayed category has subcategories in it? I mean not related to your woocommerce store settings. – Victor Aug 6 '20 at 11:57
  • Regular taxonomy archives don't ever display subcategories, so you'd never need to do it for the same reason. But if you did the only way is to use get_terms() with the parent argument set to the current term ID to see if any exist. The current taxonomy won't have any information about how many children it has. – Jacob Peattie Aug 7 '20 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.