How to add shortcode on product page by category in Woocommerce?

  php, product, shortcode, woocommerce, wordpress

I’m trying to show my own shortcode on one product page, for a specific woocommerce category. This is the code I added to function.php, but only the text is displayed and not the shortcode.

Not Working shortcode:

add_action( 'woocommerce_after_single_product_summary', 'add_text' );
function add_text() {
if ( has_term( 'mobile', 'product_cat' ) ) {
echo '[ditty_news_ticker id="17"]';
} elseif ( has_term( 'tables', 'product_cat' ) ) {
echo '[ditty_news_ticker id="18"]';
}

}

Work text

add_action( 'woocommerce_after_single_product_summary', 'add_text' );

function add_text() {

if ( has_term( 'mobile', 'product_cat' ) ) {
echo 'Something';
} elseif ( has_term( 'tables', 'product_cat' ) ) {
echo 'Something else'; 
}

}

Source: Ask PHP

LEAVE A COMMENT