Show only one product on page by specific tag in shopify - 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.

Related

Is there any way to get settings value of another snippet in shopify?

I have made a block and snippet files for each item in the block.```
{% for block in section.blocks %}
<div {{ block.shopify_attributes }}>
{% case block.type %}
{% when 'cart_header' %}
{% include 'cart_header' %}
{% when 'Countdown_Timer' %}
{% include 'Countdown_Timer' %}
{% when 'Announcement_Bar' %}
{% include 'Announcement_Bar' %}
{% when 'Free_Shipping_Bar' %}
{% include 'Free_Shipping_Bar'%}
{% endcase %}
</div>
{% endfor %}```
Is there any way or trick that I can get block.settings.xxx value of cart_header on countdown_timer snippet file?

How to exclude sold out products from related products section Shopify?

I'm trying to prevent that sold out products will be shown on related products section in my Shopify store. I tried to use {% if product.available %} in the beginning of the code but without success.
Here is the code of this section -
{% if collection and collection.all_products_count > 1 %}
{% assign col = collection.handle %}
{% else %}
{% assign col = product.collections.last.handle %}
{% endif %}
{% for tag in product.tags %}
{% if tag contains 'meta-related-collection-' %}
{% assign related_collection_handle = tag | remove: 'meta-related-collection-' %}
{% if collections[related_collection_handle].all_products_count > 0 %}
{% assign col = related_collection_handle %}
{% assign collection = collections[col] %}
{% endif %}
{% endif %}
{% endfor %}
{% if col %}
{% if collections[col].all_products_count != 1 or collections[col].products.first.id != product.id %}
{% assign skip_product = product %}
{% assign products = collections[col].products %}
{% unless sidebar %} <div class="container"> {% endunless %}
<div class="related-products__title {% unless section.settings.related_products_style == 'slider' %}{% if sidebar %}twelve columns{% else %}sixteen columns{% endif %}{% endunless %}">
<h4 class="title center">{{ 'products.product.related_items' | t }}</h4>
<div class="feature_divider"></div>
</div>
<div class="clear"></div>
{% unless sidebar %} </div> {% endunless %}
{% if section.settings.related_products_style == 'slider' %}
{% assign limit = section.settings.related_products_limit %}
<div class="related-products related-products--slider js-related-products-slider">
{% if col and collections[col].all_products_count > 0 and product.available %}
{% include 'product-slider', related_products: true %}
{% endif %}
</div>
{% else %}
{% assign limit = section.settings.related_products_limit | plus: 1 %}
{% assign products_per_row = section.settings.products_per %}
{% if col and collections[col].all_products_count > 0 and product.available %}
{% unless sidebar %}<div class="container related-products--grid">{% endunless %}
<div class="{% if sidebar %}twelve{% else %}sixteen{% endif %} columns">
{% include 'product-loop', related_products: true %}
</div>
{% unless sidebar %}</div>{% endunless %}
{% endif %}
{% endif %}
{% endif %}
{% endif %}
Hope to get some help, thank you!
Your related collection assignment looks a little bit complicated but should do the trick.
The issue comes with the second part. Once your collection is defined. This kind of code should work:
{% if col %}
<!-- First, memorize your current product handle -->
{% assign current_product_handle = product.handle %}
<!-- Then open the loop to get products inside related collection -->
{% for product in col.products %}
<!-- Filter your results to avoid identic product and out of stock products -->
{% unless product.handle == current_product_handle %}
{% if product.available %}
{{ product.title }} and other things you might want to display about related products.
{% endif %}
{% endunless %}
{% endfor %}
{% endif %}

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!

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

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.