ClawDuda
ClawDuda

Reputation: 123

Display custom content for specific product tags in Woocommerce single product pages

I use the below to add custom html or a text in WooCommerce single product pages, after short description:

function my_content( $content ) {
    $content .= '<div class="custom_content">Custom content!</div>';

    return $content;
}
add_filter('woocommerce_short_description', 'my_content', 10, 2);

How to restrict this on product pages that contain a specific product tag (not a product category)?

Upvotes: 2

Views: 1208

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

You can use has_term() WordPress conditional function, to restrict your custom content to be displayed only for products that have specific product tag(s) as follows (defining below in the function your product tag(s) term(s)):

add_filter('woocommerce_short_description', 'my_content', 10, 2);
function my_content( $content ) {
    // Here define your specific product tag terms (can be Ids, slugs or names)
    $product_tags = array( 'Garden', 'Kitchen' );

    if( has_term( $product_tags, 'product_tag', get_the_ID() ) ) {
        $content .= '<div class="custom_content">' . __("Custom text!", "") . '</div>';
    }
    return $content;
}

Code goes in functions.php file of the active child theme (or active theme). It should works.

For product categories: Replace 'product_tag' with 'product_cat'

Upvotes: 4

Related Questions