Ecommerce Shopify WordPress Discussion

How to display specific content in WooCommerce product subcategories

I'm using code that needs to display specific content on different category pages. My categories have the following structure: Man Cloth Shorts Shorts with pockets But the code below, displays content only on the parent category (Man), and the 1st level subcategory (Cloth): add_action( 'woocommerce_archive_description', 'add_slide_text',1 ); function add_slide_text() { $cat = get_queried_object(); if ( is_product_category() ) { if ( is_product_category( 'man' ) || $cat->parent === 233 ) { echo 'Hello man'; } elseif ( is_product_category( 'woman' ) || $cat->parent === 232 ) { echo 'Hello woman'; } else { echo ''; } } } How can I force it to display content on subcategories of a lower level? For example, in "Shorts" and "Shorts with pockets" (and possibly lower)? Any help is appreciated.
You can use the WordPress function cat_is_ancestor_of(). Check out the docs here: Basically, you give it two things: what you think is the parent category and what you expect to be the child category. No matter how deep the hierarchy goes, it will return true. Here's a quick example of how you can make it work: $current_category = get_queried_object(); $man_category_id = get_cat_ID('Man'); // Check if the current category or its ancestors include the "Man" category if (cat_is_ancestor_of($man_category_id, $current_category->term_id)) { echo "Display your content here"; } Just a heads up, I haven't tested this code, so feel free to adjust it to fit your situation.

December 29, 2023

You can use get_term_children() WordPress function with WooCommerce product category custom taxonomy, to display a specific text from for each specific product category "man" and "woman" terms and their children as follows: add_action( 'woocommerce_archive_description', 'add_slide_text', 1 ); function add_slide_text() { // Targeting WooCommerce product category archives if ( is_product_category() ) { $current_term = get_queried_object(); $taxonomy = $current_term->taxonomy; // For "man" term (and term ID 233) $term_man_id = get_term_by('slug', 'man', $taxonomy)->term_id; // Get "man" term ID $children_man_ids = (array) get_term_children($term_man_id, $taxonomy); // Get children terms IDs $man_terms_ids = array_merge( array(233, $term_man_id), $children_man_ids ); // Merge terms IDs in a unique array // For "woman" term (and term ID 232) $term_woman_id = get_term_by('slug', 'woman', $taxonomy)->term_id; // Get "woman" term ID $children_woman_ids = (array) get_term_children($term_woman_id, $taxonomy); // Get children terms IDs $woman_terms_ids = array_merge( array(232, $term_woman_id), $children_woman_ids ); // Merge terms IDs in a unique array // Conditional text display if ( in_array( $current_term->term_id, $man_terms_ids ) ) { _e('Hello man', 'woocommerce'); } elseif ( in_array( $current_term->term_id, $woman_terms_ids ) ) { _e('Hello woman', 'woocommerce'); } } } Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

December 29, 2023

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.