Hiding products based on customer tag Shopify search.liquid - shopify

I hope someone may be able to help with this.
I am currently setting up my store with shopify and have duplicated my products for retail and wholesale customers.
The only issue I am faced with is that the retail products are still showing when a customer with the 'wholesale' tag uses the search box.
I was wondering if I add a 'retail' tag to the relevant products, can add any code in search.liquid so that if the customer.tag contains 'wholesale' do not show products with product.tags 'retail' or something along those lines?
My current search.liquid looks like:
<!-- /templates/search.liquid -->
{% comment %}
To return only products or pages in results:
- http://docs.shopify.com/manual/configuration/store-customization/return-only-product-in-storefront-search-results
- Or manually add type=product or type=page to the search URL as a parameter
{% endcomment %}
{% comment %}
Check to enforce respond.js
{% endcomment %}
{% assign respond_js_secret_key = shop.domain | md5 %}
{% unless search.terms == respond_js_secret_key %}
{% comment %}
Avoid accessing search.results before the opening paginate tag.
If you do, the pagination of results will be broken.
{% endcomment %}
{% paginate search.results by 12 %}
<div class="grid">
<div class="grid__item">
<header class="section-header text-center">
{% if search.performed %}
{% if search.results_count == 0 %}
<h1 class="text-center">{{ 'general.search.no_results_html' | t: terms: search.terms }}</h1>
{% else %}
<h1 class="text-center">{{ 'general.search.results_for_html' | t: terms: search.terms }}</h1>
{% endif %}
{% else %}
<h1 class="text-center">{{ 'general.search.title' | t }}</h1>
{% endif %}
<hr class="hr--small">
</header>
{% include 'search-bar', search_btn_style: 'btn', search_bar_location: 'search-bar--page' %}
{% if search.performed %}
<hr class="hr--medium hr--clear">
<div class="grid-uniform">
{% for item in search.results %}
{% assign itemIswholesale = false %}
{% if item.tags contains 'wholesale' or item.title contains 'wholesale' %}
{% assign itemIswholesale = true %}
{% endif %}
{% if itemIswholesale and customer and customer.tags contains 'wholesale' %}
{% if item.object_type == 'product' %}
{% assign product = item %}
{% include 'product-grid-item' %}
{% else %}
<div>
<div>
<a href="{{ item.url }}">
<span>
<span>{{ item.title }}</span>
{{ item.content | strip_html | truncatewords: 60 }}
</span>
</a>
</div>
</div>
{% endif %}
{% else %}
{% unless itemIswholesale %}
{% if item.object_type == 'product' %}
{% assign product = item %}
{% include 'product-grid-item' %}
{% else %}
<div>
<div>
<a href="{{ item.url }}">
<span>
<span>{{ item.title }}</span>
{{ item.content | strip_html | truncatewords: 60 }}
</span>
</a>
</div>
</div>
{% endif %}
{% endunless %}
{% endif %}
{% endfor %}
</div>
{% if paginate.pages > 1 %}
{% include 'pagination' %}
{% endif %}
{% endif %}
</div>
</div>
{% endpaginate %}
{% else %}
{% include 'respond' %}
{% layout none %}
{% endunless %}
I am a complete novice and have managed to get by this far following help and tutorials online so any help would be very much appreciated.
I can't afford to subscribe to an additional app at present, such as locksmith and would really like to keep control so I can continue administration in future,
Thanks in advance,

You may try doing something like that inside the
{% if search.performed %}
condition.
First get some information about user and store it:
{% assign wholesale = false %}
{% if customer %}
{% assign customer_tags = customer.tags | split: "," %}
{% if customer_tags contains 'wholesale' %}
{% assign wholesale = true %}
{% endif %}
{% endif %}
Explanations : first you assign a false statement to the wholesale status. Then you check if it is customer ( no need to go further if user is not connected). If he is, then you check if he has a wholesale tag. If he is you assign a true statement.
Then you are able to display something different this way:
{% for result in search.results %}
{% if wholesale %}
Do something
{% else %}
Do something else
{% endif %}
{% endfor %}
Please not that you may have some issues with pagination.

Related

Shopify search for related products is not returning similar tagged products

I have a custom module that returns the first 4 related products by tag. It works on ALMOST products except two tags and I can't figure out why. Excluding variants , there are only 108 products site wide. I am displaying the product tag right above the module for you to see as well.
Sample product page with working tag (PowerChairs)
https://www.gallantmedicalsupply.com/collections/power-chairs/products/compass-sport-power-chair
Sample product page with non-working tag (Walkers)
https://www.gallantmedicalsupply.com/products/walk_002
{% assign product = product %}
{% assign current_tags = product.tags %}
{% assign all_products = collections.all.products %}
<div class="container custom-related-product">
<section>
<div>
{% if section.settings.product_recommendations_heading != blank %}
<div class="twelve{% unless settings.borders_enable %} bottompad{% endunless %}">
<h2 class="text-center">{{ section.settings.product_recommendations_heading }}</h2>
{% echo current_tags %}
</div>
{% endif %}
{% assign related_products = 0 %}
<div class="product-loop center">
{% for similar_product in all_products %}
{% if similar_product.handle != product.handle %}
{% for current_product_tag in product.tags %}
{% if similar_product.tags contains current_product_tag %}
{% render 'product.loop', product: similar_product, block_width: 'three' %}
{% assign related_products = related_products | plus: 1 %}
{% break %}
{% endif %}
{% endfor %}
{% if related_products == 4 %}
{% break %}
{% endif %}
{% endif %}
{% endfor %}
</div>
</div>
</section>
</div>
Due to the loop limit of 50 products. I filtered the all.products by collection first (excluding all collection and current product) and then iterating to render.
{% if collection == null or collection.handle == 'frontpage' or collection.handle == 'all' %}
{% assign found_a_collection = false %}
{% for c in product.collections %}
{% echo c %}
{% if found_a_collection == false and c.handle != 'frontpage' and c.handle != 'all-products' and c.all_products_count > 1 %}
{% assign found_a_collection = true %}
{% assign collection = c %}
{% endif %}
{% endfor %}
{% endif %}
<div class="container custom-related-product">
<section>
<div>
{% if section.settings.product_recommendations_heading != blank %}
<div class="twelve{% unless settings.borders_enable %} bottompad{% endunless %}">
<h2 class="text-center">{{ section.settings.product_recommendations_heading }}</h2>
</div>
{% endif %}
<div class="product-loop center">
{% for similar_product in collection.products %}
{% if similar_product.handle != product.handle %}
{% render 'product.loop', product: similar_product, block_width: 'three' %}
{% assign related_products = related_products | plus: 1 %}
{% if related_products == 4 %}
{% break %}
{% endif %}
{% endif %}
{% endfor %}
</div>
</div>
</section>
</div>

How to get all tags of all collections products with tag link

I need to fetch all the tags on my collection page and display it within tag link.
I use this below code :
{% assign collection = collections.all %}
{% paginate collection.products by 1000 %}
<h3>All Tags</h3>
<div id="tags">
{% if collection.tags.size == 0 %}
No tags found.{% else %}
{% for tag in collection.tags %}
<a href="{{ collection.url }}/{{ tag | handle }}">
{{ tag }}
</a>
{% unless forloop.last %}, {% endunless %}
{% endfor %}
{% endif %}
</div>
{% endpaginate %}
Please help me to solve my problem.
I think you need to use a built-in solution rather than paginate
use like it
<h3>All Tags</h3>
<div id="tags">
{% if collection.all_tags.size == 0 %}
No tags found.
{% else %}
{% for tag in collection.all_tags %}
{{ tag | link_to_tag: tag }}
{% unless forloop.last %}, {% endunless %}
{% endfor %}
{% endif %}
</div>
please replace below code :
{{ tag }}
To this :
{{ tag | link_to_tag: tag }}

Jekyll get tag at index

I'm teaching myself Jekyll and Liquid and was wondering how do you get indexed items of an array?
I can create an array of page.tags and loop through them:
{% assign tags = pages.tags %}
{% for tag in tags %}
{% endfor %}
But say there are four tags and I want to access tag at index 2. I've seen some code like this:
{% for i in 1...page.tags %}
{% endfor %}
But I can't seem to get the index to work, these fail:
{% for i in 1...page.tags %}
<p>{{page.tags[i]}}</p>
{% endfor %}
{% for i in 1...page.tags %}
<p>{{i}}</p>
{% endfor %}
I improved your code slightly:
<div id="topNav">
<ul>
{% for tag in page.tags %}
{% if forloop.first %}
<li class="fadeIn firstItem notLogo">{{tag}}</li>
{% else %}
<li class="fadeIn notLogo">{{tag}}</li>
{% endif %}
{% endfor %}
</ul>
</div>
Source: https://help.shopify.com/en/themes/liquid/objects/for-loops
I've solved it with a kind of hack.
<div id="topNav">
<ul>
{% assign count = 0 %}
{% for tag in page.tags %}
{% if count == 0 %}
<li class="fadeIn firstItem notLogo">{{tag}}</li>
{% else %}
<li class="fadeIn notLogo">{{tag}}</li>
{% endif %}
{% assign count = count | plus: 1 %}
{% endfor %}
</ul>
</div>

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 Liquid error: Memory limits exceeded

I was trying to display all the product types of my site in one page , 2 days back it was listing all the all the product type.
But now when that page is loads its giving error like "shopify Liquid error: Memory limits exceeded"
Here is my code
<div class="rte">
{{ page.content }}
<ul class="vendor-list block-grid three-up mobile one-up">
{% for product_type in shop.types %}
{% assign its_a_match = false %}
{% capture my_collection_handle %} {{ type | handleize | strip | escape }} {% endcapture %}
{% assign my_collection_handle_stripped = my_collection_handle | strip | escape %}
{% for collection in collections %}
{% if my_collection_handle_stripped == collection.handle %}
{% assign its_a_match = true %}
{% endif %}
{% endfor %}
{% if its_a_match %}
<li class="vendor-list-item">{{ product_type }}</li>
{% endif %}
{% endfor %}
</ul>
</div>
how can i overcome this problem ?
Try the following. It's faster and more efficient.
<div class="rte">
{{ page.content }}
<ul class="vendor-list block-grid three-up mobile one-up">
{% for product_type in shop.types %}
{% assign type = product_type | strip | escape | handleize %}
{% assign collection = collections[type] %}
{% if collection.handle != '' %}
<li class="vendor-list-item">{{ product_type }}</li>
{% endif %}
{% endfor %}
</ul>
</div>