How to display products from specific tag in shopify? - shopify

How to display products from specific tag "organic" in shopify custom template? Here is my code.
{% if customer.tags contains 'organic' %}
<div class="product-list product-list--collection">
{%- for product in collections.all.products limit: product_limit-%}
{% for c in product.collections %}
{% if c.title == collection.title %}
{%- render 'product-item' -%}
{% endif %}
{% endfor %}
{% if collection.handle=="all" %}
{%- render 'product-item' -%}
{% endif %}
{%- endfor -%}
</div>
{% else %}
<div class="product-list product-list--collection
{% if has_filters %}
product-list--with-sidebar
{% endif %}">
{%- for product in collection.products -%}
{%- render 'product-item' -%}
{%- endfor -%}
</div>
{% endif %}

{% for product in collections.all.products %}
{% if product.tags contains "organic" %}
{% include 'product-list-item' %}
{% endif %}
{% endfor %}

it will help in displaying the specific product title ,
Please also check the API documentation here
{% for tag in customer.tags %}
{% if tag == 'organic' %}
{% assign collection = collections[my-collection-handle] %}
{% paginate collection.products by 99 %}
{% assign products = collection.products %}
{% if collection.products.size > 0 %}
{% for product in products %}
{% for tag in product.tags %}
{% if product.tag == 'limited_edition' %}
<p>{{ product.title }}</p>
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% endpaginate %}
{% endif %}
{% endfor %}

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 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.

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: 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.