1

In WooCommerce, archive-product is the default template for all product categories. I want to create a new product category template for one parent category and all of its children.

I've created a new template archive-foo.php

Now I need to change the code inside taxonomy-product_cat.php and tell it to use archive-foo.php "if it's my parent category or any of its children".

Here is my code inside taxonomy-product_cat.php:

if (is_product_category( 'foo-category' ) || cat_is_ancestor_of(19, get_queried_object()->term_id )){ wc_get_template( 'archive-foo.php' );
    } else { wc_get_template( 'archive-product.php' ); }

Currently, it's working for the parent category, but none of its children. Any help would be greatly appreciated.

0

the WordPress function cat_is_ancestor_of() is made for WordPress categories, but not for WooCommerce product categories which is a custom taxonomy product_cat.

For Custom taxonomies you can use WordPress term_is_ancestor_of() function like:

term_is_ancestor_of( 19, get_queried_object_id(), 'product_cat' )

So in your code:

if ( is_product_category( 'foo-category' ) || term_is_ancestor_of( 19, get_queried_object_id(), 'product_cat' ) ) { 
    wc_get_template( 'archive-foo.php' );
} else { 
    wc_get_template( 'archive-product.php' ); 
}

This time it should work.

  • This worked perfectly, thank you very much for taking the time to help! – combatdev Aug 9 at 16:50

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.