skip to Main Content

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…

2

Answers


  1. How do you will obtain the ids of those products? If there are just a few id’s, you can just add them with an OR Statement.

    add_action( 'woocommerce_single_product_summary', 'return_policy', 20);
    function return_policy() {
    if ( is_single( '85' )||is_single( '1077' )) {
        echo '<p>This will run on products 85 and 1077</p>';
        }
    } 
    

    This is how it looks on my test site.
    enter image description here
    enter image description here

    Login or Signup to reply.
  2. 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>';
       }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search