John Davidson

php - Woocommerce | display different text on product page if product has tag "xyz"

0 comments
Message:


I want to put a text or html under the short product description

Normally I can put any text in the short-description.php file
But I want this text to be displayed only when my product has a Special tag and also the user is not registered


For example, when my product has a "Free" tag, show the user the following text in short description


Please register to purchase the product for free

And when the user is registered, do not show the text to him



My Answer:
You can achieve this by adding the following code to your theme's functions.php file:

php
add_action( 'woocommerce_before_single_product_summary', 'display_custom_text_based_on_product_tag', 5 );

function display_custom_text_based_on_product_tag() {
global $product;

if ( has_term( 'your-tag-slug', 'product_tag', $product->get_id() ) ) {
echo '

This product has the tag "your-tag-slug". Display this text.

';
} else {
echo '

This product does not have the tag "your-tag-slug". Display this text.

';
}
}


Replace 'your-tag-slug' with the actual slug of the tag you want to check for. This code will display different text on the product page based on whether the product has the specified tag or not.

Rate this post

3 of 5 based on 2270 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us