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.
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.
Share Improve this question edited Aug 9, 2019 at 16:51 LoicTheAztec 3,39117 silver badges24 bronze badges asked Aug 9, 2019 at 15:07 combatdevcombatdev 135 bronze badges1 Answer
Reset to default 1the 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.