How to exclude product vendors from search results by customer tags - shopify

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 %}

Related

How do I write a condition on a Django template that says if this item was the last item to match this condition

In Django, in Temple,
I want to say if this was the last item that equal the if condition,
Do not leave a comma at the end.
What should I write instead of if not forloop.last?
{% for item in filters %}
{% for i in username_filter %}
{% if i.order == item.order %}
{% if not forloop.last %}
{{ i.sender_choice }},
{% else %}
{{ i.sender_choice }}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}

How to display products from specific tag in 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 %}

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.

Break out of two loops in Shopify Liquid

How do I break out of two loops in liquid. So far I have, which doesn't seem to work for me.
{% for x in a %}
{% for y in b %}
{% if y = 2 %}
{% break %}
// When this loop breaks, the parent for loop should also break
{% endif %}
{% endfor %}
{% endfor %}
You can add a flag and check once that is changed.
{% assign break_loop = false %}
{% for x in a %}
{% for y in b %}
{% if y = 2 %}
{% break %}
{% assign break_loop = true %}
// When this loop breaks, the parent for loop should also break
{% endif %}
{% endfor %}
{% if break_loop %}
{% break %}
{% endif %}
{% endfor %}

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.