I have the following code in my child theme’s functions.php file, and it works great.
function themeprefix_custom_price_message( $price ) {
global $post;
$product_id = $post->ID;
$my_product_array = array( 270,373,378,506,1306,1311,1312,1444,1445,1447,1449,1930,1932,1933,1934,1935,1963,4146,4152,4153,4154 );//add in product IDs
if ( in_array( $product_id, $my_product_array )) {
$textafter = ' Each/Min 12'; //add your text
return $price . '<span class="price-description">' . $textafter . '</span>';
}
else
{
return $price;
}
}
add_filter( 'woocommerce_get_price_html', 'themeprefix_custom_price_message' );
However, now I need to add another group of products and enable different text after the price, i.e., Each/Min 8. I have tried various changes to the above and have brought the site down each time. How do I adjust the above to add another (and then possibly another) group of product IDs with different text after the price? Thanks in advance!
2
Answers
You could leave the existing array as it is. Simplify the code by removing the
else
from theif
and then start declaring & checking new product arrays before returning the default price-only text…This answer is for the question which was asked here: Add custom text after the price in WooCommerce
However, it can also be used in the above question.
You can try something like that if you want dynamic text after every product.
You can see the above changes in your product.
Now add the below code to save meta in your product
And now to display custom code on the front end you need to use 2 hooks.
In the front end, it will look like this.