Shopify Render some HTML only if product has certain tag - shopify

Shopify Render some HTML only if the product has a certain tag
THE Problem is if the tag is 2 times it duplicates
if i hvae 'new' and 'new 1'
how to limit to show 1-time per page
{% for tag in product.tags %}
{% if tag contains 'new' %}
<div class="new-tag new-tag--absolute"> NEW </div>
{% endif %}
{% endfor %}

Use {% break %} to exit a for loop. Once you've discovered one 'new' tag, you can exit your loop.
{% for tag in product.tags %}
{% if tag contains 'new' %}
<div class="new-tag new-tag--absolute"> NEW </div>
{% break %}
{% endif %}
{% endfor %}

Related

Display HTML if any item of cart got specific tag

I want to implement two selectboxes in my shopify cart page if the cart contains an item with tag "test1"
my current code
{% for item in cart.items %}
{% for products in item.products %}
{% for tag in product.tags %}
{% if tag contains 'test1' %}
<select>....</select>
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
anyone can tell me what I'm doing wrong?
Please refer below code. you are rendering unnecessary loops. item access product object directly and product can tags.
{% for item in cart.items %}
{% if item.product.tags contains 'custom-tag' %}
<h1> Yahoo </h1>
{% endif %}
{% endfor %}
O/P will like below Image

Shopify (Liquid) if product has a tag, echo that tag

I'm trying to find product tags that meet a condition, and then display those tag(s).
This gets me all the tags for a product...
{% for tag in product.tags %}
{% if product.tags contains '336699' %}
{{ tag }}
{% endif %}
{% endfor %}
How can I just echo the tags that meet the 'contains' criteria?
You're almost there. Use for to iterate the product.tags and contains within a if to match tag with your string literal criteria.
{% for tag in product.tags %}
{% if tag contains '336699' %}
{{ tag }}
{% endif %}
{% endfor %}

Show only one product on page by specific tag in shopify

<div class="row">
{% for product in collections.all.products %}
{% if product.tags contains 'frontpagedeal' %}
{% if product.price_min < product.compare_at_price_min %}
{% include 'today-sale' %}
{% endif %}
{% endif %}
{% endfor %}
</div>
I try below code but this is also not working.
<div class="row">
{% for product in collections.Daily_Deals.products %}
{% if product.tags contains 'frontpagedeal' %}
{% include 'dailydeal-countdown' %}
{% endif %}
{% endfor %}
</div>
how I can display a only one product with specific tag ??
{% for product in collections.Daily_Deals.products %}
{% if product.tags contains 'frontpagedeal' %}
{% if forloop.first == true %}
{% include 'dailydeal-countdown' %}
{% endif %}
{% endif %}
{% endfor %}
Can you please test this and tell me if this is what you want.
all i did is adding {% if forloop.first == true %} .... {% endif %} which will display only one product.
In your second example handles are always lower case so:
<div class="row">
{% assign oneShown = false %}
{% for product in collections.daily_deals.products %}
{% if oneShown %}
{% break %}
{% endif %}
{% if product.tags contains 'frontpagedeal' %}
{% include 'dailydeal-countdown' %}
{% assign oneShown = true %}
{% endif %}
{% endfor %}
</div>
Should work. However if your collection has more than 50 products you may not find the product you are looking for so you'll need to make sure the collection has fewer than 50 products. Ideally for something like this "Daily Deals" would be a smart collection that includes products based on them being a 'frontpagedeal' and you can skip the tag check.
Maybe it would be useful to create a "sale collection" directly in store administration and display it then.
If it is for a client, you may add an option to theme to allow client to select sale collection in theme settings.

Shopify check for for tag [liquid]

{% for tag in product %}
{% if product.tags contains new %}
<span>Test</span>
{% endif %}
{% endfor %}
I need to look in products, and if a product has a tag called new then display a span tag if not then nothing.
For some reason, this piece of code doesn't work.
This can do the job :
{% for product in products %}
{% if product.tags contains "new" %}
<span>Test</span>
{% endif %}
{% endfor %}

Shopify - How to exclude a collection in paginate?

I need to exclude a collection inside the paginate loop. But if I use the conditional statement, it will still be counted in paginate count.
So If I paginate by 10 and there are 2 products from the excluded collection in first page, I will only see 8 products in first page.
Any solution for this? Here's my snippet:
{% paginate collection.products by 10 %}
<ul>
{% for product in collection.products %}
<!-- Check for collection -->
{% assign is_treatment = false %}
{% for c in product.collections %}
{% if c.handle == "salon-treatment" %}
{% assign is_treatment = true %}
{% endif %}
{% endfor %}
{% unless is_treatment %}
<li>{{ product.title }}</li>
{% endunless %}
{% endfor %}
</ul>
{% endpaginate %}
Note: This question is duplicated from the one I posted in Shopify forum.
What I did for something like this was first load in all the desired products into a collection of my own (basically an array of objects), and then paginate THAT array... so, if you write a function called exclude_some_products which returns all the unexcluded products, do this:
{% assign my_smaller_collection = exclude_some_products collection %}
{% paginate my_smaller_collection.products by 10 %}
<ul>
{% for product in my_smaller_collection.products %}
<!-- Check for collection -->
{% assign is_treatment = false %}
{% for c in product.collections %}
{% if c.handle == "salon-treatment" %}
{% assign is_treatment = true %}
{% endif %}
{% endfor %}
{% unless is_treatment %}
<li>{{ product.title }}</li>
{% endunless %}
{% endfor %}
</ul>
{% endpaginate %}
p.s. pardon my code, I'm not even sure what language this is!