Skip to content
Advertisement

Woocommerce: add text before add-to-cart only on specific single product pages

I found the following code in order to display text before the add-to-cart-button on a single product page:

// adds notice at single product page above add to cart

add_action( 'woocommerce_single_product_summary', 'return_policy', 20);
function return_policy() {
if ( is_single( '11675' ) ) {
    echo '<p>My Text</p>';
    }
} 

This works perfect!

However, I would like to show this message not only on one product page (by id), but on several product pages (by serveral ids), therefore I have to insert multiple ids. How does the above code have to changed, in order to achive this? From my barely knowledge I think that one have to insert so called arrays – but unfortunately, I do not know how to code this…

Advertisement

Answer

if you want to use it with the help of an array then use it like this

add_action( 'woocommerce_single_product_summary', 'return_policy', 20);
function return_policy() {
   global $post;
   // create array of ids on which you want to show it
   $idsArr = [100, 105];
   if ( is_single() && in_array($post->ID, $idsArr )) {
      echo '<p>Text goes here</p>';
   }
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement