What's your biggest current challenge? Have your say in Community Polls along the right column.

Re: if contains is not working with variable

Why is 'contains' not working with variables in product tags?

chandra_mmbo
Visitor
3 0 0

{% if product.tags contains prod.title %} is not working. If I pass the static value with quote like- {% if product.tags contains '467455' %} , it is working but pass the variable, not working. Please guide.

 

 

<ul class="related-products">
{% paginate collections.all.products by 1000 %}
{% for prod in collections.all.products %}

{%- comment -%}here the issue if I use static value like='785643754', it is working but when pass the variable. it is not working {%- endcomment -%}


{% if prod.tags contains product.id %} 
<li>
<div class="image">
<a href="{{ prod.url | within: collections.all }}" title="{{ prod.title | escape }}">
{{ prod.featured_image | product_img_url: image_size | img_tag }}
</a>
</div>
<h1 class="product-title" itemprop="name">
<a href="{{ prod.url | within: collections.all }}" title="{{ prod.title | escape }}">{{ prod.title }}</a>
</h1>
<h2 class="product-price" itemprop="price">
<span id="ProductPrice">{{ prod.price | money }}</span>
{%- if prod.compare_at_price > prod.price -%}
<span id="ComparePrice">
<s class="reduced-price">{{ prod.compare_at_price | money }}</s>
</span>
{%- endif -%}
</h2>

</li>
{% capture counter %}{{ counter }}*{% endcapture %}
{% endif %}

{% endfor %}
{% endpaginate %}
</ul>

 

Please help that how to fix. Its urgent

 

**Unsolicited mentioned removed by Community Moderator**

cb
Replies 5 (5)

Kani
Shopify Partner
468 125 232

Hi @chandra_mmbo 

 

I have read your code. And can't find '{% if product.tags contains prod.title %}'.

Most close one is '{% if prod.tags contains product.id %} ', and the problem is 'product.id', you have not define 'product', so it should be 'prod.id'

 

Hey!! ʕ ᵔᴥᵔ ʔ
Please click 'Like' and ' Accept as Solution' to encourage me to continue sharing my expertise. ♡
If you need any technical assistance, feel free to send me a DM. You no longer have to search for answers without getting a response. 🙂
chandra_mmbo
Visitor
3 0 0

Hi @Kani 

 

I have created a liquid file where I want to show related products by tags of current product. So I fetch all products  by  {% for prod in collections.all.products %}.


Now here, I am finding which products have current product id in tags(all products) {% if prod.tags contains product.id %}, but result not found. 

 

{%- comment -%}{% if prod.tags contains product.id %} current product id = 8201838624999 {%- endif -%} {%- endcomment -%}


{% if prod.tags contains '8201838624999' %}  - when replace the product.id with static value of current product, its working. 

 

 

cb
Kani
Shopify Partner
468 125 232

Then I think you need try to print {{ product.id }}

Hey!! ʕ ᵔᴥᵔ ʔ
Please click 'Like' and ' Accept as Solution' to encourage me to continue sharing my expertise. ♡
If you need any technical assistance, feel free to send me a DM. You no longer have to search for answers without getting a response. 🙂
chandra_mmbo
Visitor
3 0 0

{{ product.id }}  

I print the product.id, it is displaying but in condition, not working. 

 

While I am using {% if prod.type contains product.id %}. It is working.  I am confuse 

 {{ prod.tags | json }} - ["8201838624999","Prints"]

 

cb

MiracleWebsoft
Shopify Partner
38 4 10

I found the solution for this. Actually you are comparing a string with an integer. Both type should be the same when we use contains. Try the below code it will work

 

Here converted integer to string so contain work 

{% assign product_id = product.id | append: "" %}

You can use like this

{% for prod in collections.all.products %}

{% assign product_id = product.id | append: "" %}
{% if prod.tags contains product_id %}
<h1>Contain</h1>
{% else %}
<h1>Not Contain</h1>
{% endif %}

{% endfor %}

 

- Your Coffee Tip can create magic in coding


- Seeking a Shopify Certified Developer? Contact us Shopify Verified partners



If I managed to help you then, don't forget to Like it and Mark it as Solution! 