Shopify: How would I see if a product in my cart had a specific tag - shopify

I want to check if a item in my check out cart is a part of a collection(or if it has a specific tag).
Here is my "attempt" at trying to check if there is a product
{% for item in cart.items %}
{% if item.product.collections == "SampleProduct" %}
<p>Enjoy your Product</p>
{% endif %}
{% endfor %}
Any help will be appreciated.
Thanks

If you want to check if your product is in a specific collection, you'll need something like this:
{% for item in cart.items %}
{% assign found_collection = false %}
{% for collection in item.product.collections %}
{% if found_collection == false and collection.title == 'SampleProduct' %}
{% assign found_collection = true %}
{% endif %}
{% endfor %}
{% if found_collection %}
<p>Enjoy your Product</p>
{% endif %}
{% endfor %}
Or if you want to check for a tag, use this:
{% for item in cart.items %}
{% if item.product.tags contains 'SampleProduct' %}
<p>Enjoy your Product</p>
{% endif %}
{% endfor %}
For more info, see the Shopify wiki page for the contains operator.

Related

How to exclude product vendors from search results by customer tags

I am trying to exclude products based on their vendor type for each customer tag. For example, if a customer is signed in and tagged as 'designer' they should only gets products from vendor type 'designer' in the search results.
I put the code below inside of {% if search.performed %} but it isn't doing the trick. Thanks in advance.
{% assign isWholesaler = false %}
{% if customer %}
{% if customer.tags contains 'wholesale' %}
{% assign isWholesaler = true %}
{% endif %}
{% endif %}
{% if isWholesaler %}
{% for result in search.results %}
{% if item.product.vendor == 'CompanyName Wholesale' %}
{% include 'search-result' %}
{% else %}
{% endif %}
{% endfor %}
{% endif %}
{% assign isDesigner = false %}
{% if customer %}
{% if customer.tags contains 'designer' %}
{% assign isDesigner = true %}
{% endif %}
{% endif %}
{% if isDesigner %}
{% for item in search.results %}
{% if item.product.vendor == 'CompanyName Designer' %}
{% include 'search-result' %}
{% else %}
{% endif %}
{% endfor %}
{% endif %}
{% assign isConsumer = false %}
{% if customer %}
{% if customer.tags contains 'consumer' %}
{% assign isConsumer = true %}
{% endif %}
{% endif %}
{% if isConsumer %}
{% for item in search.results %}
{% if item.product.vendor == 'CompanyName' %}
{% include 'search-result' %}
{% else %}
{% endif %}
{% endfor %}
{% endif %}

Need help displaying related products by tag instead of collection

I have hundreds of products in a collection, I want to group the items in that collection with similar tags
How do i shows all products with matching tags in the related slider ?
Here's what I've tried so far, i managed to group together all related products in the collection:
{% if settings.show_related_product %}
<div id="related_item" class="home-carousel">
{% if collection == null or collection.handle == 'frontpage' or collection.handle == 'all' %}
{% assign found_a_collection = false %}
{% for c in product.collections %}
{% if found_a_collection == false and c.handle != 'frontpage' and c.handle != 'all' and c.all_products_count > 1 %}
{% assign found_a_collection = true %}
{% assign collection = c %}
{% endif %}
{% endfor %}
{% endif %}
{% if collection and collection.products_count > 1 %}
{% if settings.heading_related_product != blank %}
<h4>{{ settings.heading_related_product }}</h4>
{% endif %}
<div class="related-items">
{% assign current_product = product %}
{% assign current_product_found = false %}
{% for product in collection.products limit: settings.related_product_number %}
{% if product.handle == current_product.handle %}
{% assign current_product_found = true %}
{% else %}
{% unless current_product_found == false and forloop.last %}
<div class="related-item">
{% include 'product-item' with collection.handle %}
</div>
{% endunless %}
{% endif %}
{% endfor %}
</div>
{% endif %}
</div>
{% endif %}
{% if settings.show_wear_with and settings.wear_with_col != blank%}
<div id="wear_with_item" class="home-carousel">
{% assign wearwithCol = collections[settings.wear_with_col] %}
{% assign wearwithNum = settings.wear_with_number %}
{% if settings.heading_wear_with != blank %}
<h4>{{ settings.heading_wear_with }}</h4>
{% endif %}
<div class="wear-with-items">
{% for product in wearwithCol.products limit: wearwithNum %}
<div class="wear-with-item">
{% include 'product-item' %}
</div>
{% endfor %}
</div>
</div>
{% endif %}
When you need to filter products by tag, the best solution is to use JS, since liquid doesn't have a native way to do so.
So the best way is to target the /collections/all/TAG page and get the products from there with an AJAX request, since they will be filtered by tags on that page.
So something like so:
fetch('/collections/all/SOME_TAG')
.then(r => r.json())
.then(data => /* Do Something With The data */)
If you really really want to use only liquid code than you can do the following "hack":
{% paginate collections.all.products by 9999 %}
{% for _product in collections.all.products %}
{% if _product.tags contains 'SOME_TAG' %}
Do something here
{% endif %}
{% endfor %}
{% endpaginate %}
But I wouldn't recommend the liquid only solution since that will make the server time load increase quite significantly.

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!