How to set up a non-matching condition in liquid for product handles?

Hi!

I’m having trouble setting up a condition where liquid prints an output when the products (product.handle) inside an array do not match the current product page (product.handle) i’m on; eg when the current product.handle is not present in the array. Could anybody give me some insights?

{% assign promotionProducts = shop.metafields.promotion.products | split: ',' %}
{% assign salesprice = product.price | divided_by: 100 | times: shop.metafields.promotion.discount %}                                   
{% for handle in promotionProducts %}
{% assign promotion_product = all_products[handle] %}
{% if promotion_product.handle == product.handle %}
{{ product.price | minus: salesprice | money }}
{{ product.price | money }}
{% else %}
not in array = the current product.handle is not present in the promotionProducts array
{% endif %}
{% endfor %}

Try this

{% if promotion_product.handle contains product.handle %}

That doesn’t help, it’s the same output as with “==”.

{% assign promotionProducts = shop.metafields.promotion.products | split: ',' %}
{% assign salesprice = product.price | divided_by: 100 | times: shop.metafields.promotion.discount %}                                   
{% for handlecutom in promotionProducts %}
{% assign promotion_product = all_products[handlecutom ] %}
{% if promotion_product.handlecutom == product.handle %}
{{ product.price | minus: salesprice | money }}
{{ product.price | money }}
{% else %}
not in array = the current product.handle is not present in the promotionProducts array
{% endif %}
{% endfor %}

If still don’t work then share where you are using it and show the output on the page of this variable
{% assign promotion_product = all_products[handlecutom ] %}

With your new code, all product pages end up in the {% else %}, where as my initial correctly targetted the matching products. The output of promotion_product.handle correctly shows the (currently 3) products in the array.

I use this code in product.liquid. My goal is to use various metafields (promotion.is_active, promotion.products and promotion.discount) in order to show a discounted price only on the matching product pages which are in de metafield promotion.products array. The input of the promotion.products is producthandle, producthandle, producthandle, … So i’m matching the producthandle from inside promotion.products with the current producthandle (on the product page itself) with the loop. The if clause “{% if promotion_product.handle == product.handle %}” works perfectly, but i fail to generate an “else” condition to NOT show the discounted price on product pages that are not in that promotion.products array.

Why are you using all_products here? That feels like overkill if your metafield is just a string like “handle1,handle2,handle3”.

By splitting the string you can just compare that string (“handle2”) against the current product handle without having to invoke all_products.

And if you find a match I would have expected you just break the loop. Now I could also not be understand the need but would see something closer to this being what you’re looking for.

{% assign promotionProducts = shop.metafields.promotion.products | split: ',' %}
{% assign salesprice = product.price | divided_by: 100 | times: shop.metafields.promotion.discount %}                                   
{% for promoHandle in promotionProducts %}
  {% if promoHandle == product.handle %}
    ... your code
  {% break %}
  {% endif %}
{% endfor %}

Hi @MathijsDelva ,

To my understanding, the promotion products handles are stored in the following format: “producthandle1, producthandle2, producthandle3”. If you split the values by “,”, you’ll end up having an array with the following items:

  1. “producthandle1”;

  2. " producthandle2" (note the space before the actual product handle);

  3. " producthandle3" (note the space before the actual product handle).

As you can see, the majority of items would have a redundant space right before the actual product handle. It’s not good because “producthandle2” and " producthandle2" (with an extra space) are completely different strings. You may want to split the values by ", " (comma and space) instead of “,” (comma) and see if the redundant space was the reason of this problem.