0

The edit is at ThemeName/WooCommerce/cart/cart.php, line 149.

Extra text will be shown just above the "Coupon code" field if listed product is in the cart. This text is to inform the customer that they are eligible for a coupon (price deduction) if the condition is met.

What I've tried:

<p>
  <?php
    $product_id = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : 
             $cart_item['product_id'];

    if($product_id == "15502" || $product_id == "15566" || $product_id == "15567" || 
        $product_id == "18946" || $product_id == "18947" || $product_id == "18948" || 
        $product_id == "16199")
      echo 'You may use the coupon "x" for your free complimentary greeting card!';
  ?>
</p>

There is an empty paragraph of where the text is meant to be displayed, but the code does not work as intended.

What I was expecting: Identify product_id in cart. IF product exists in cart, then display text.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
  • On line no 146 `$cart_item` variable is not available, when you foreach `WC()->cart->get_cart()` then you get `$cart_item` so basically, you'll have to foreach then from each cart item to get the product ID and match the product from your products ids then set the true/false flag in a variable, then outside the foreach check the flag value if it's true then print your message. – Vijay Hardaha Nov 28 '22 at 05:18
  • Thank you for your reply. Based on your kind inputs, I tried this: `foreach(WC()->cart->get_cart() as $cart_item){ $product_id = $cart_item['data']; } $x = array("15502", "15566", "15567", "18946", "18947", "18948", "16199"); if($product_id == $x){ echo 'Insert text here';} ` There are no errors, but it does not output as I expected to. Could you guide me further? – TurnItOffAndOnAgain Nov 29 '22 at 08:22
  • 1: `$cart_item['data']` will give your product object not id. for id you need to use `$cart_item['product_id']` 2. You can't compare an integer with array using `$product_id == $x` you'll have to use `in_array( $product_id, $x, true )` – Vijay Hardaha Nov 29 '22 at 08:24
  • Thank you so much for your help & patience. The code does indeed work after I've edited it based on your advice. However, the code does not detect the `product_id` when it is automatically added into the cart. My site uses E&P Form Builder to enable customers to curate their own gift set. At the end of the process, their cart will be populated accordingly. The code works if I added the specific `product_id` into the cart manually. The text shows. Any ideas on why this happens, Vijay? – TurnItOffAndOnAgain Nov 30 '22 at 01:26

1 Answers1

0
<p>
  <?php
  //Check items in cart
  foreach(WC()->cart->get_cart() as $cart_item){
    $product_id = $cart_item['product_id'];
}

  $x = array(15502, 15566, 15567, 18946, 18947, 18948);

  //If this is true, then
  if(in_array($product_id, $x, true)){
    echo 'Insert text here.';
} 
  ?>
</p>

For a more functional & automated solution, please read: Auto apply or remove a coupon in Woocommerce cart for a specific product id