How to combine for and where clause in Liquid - shopify

I'm currently working on a shopify site and need to create a filter on a products page. My goal is to have primer paints show up in a different section of the product collection page. What I'm having trouble with is how to filter for primers. We have a tag set up in the product page named "Primer". What I want is for the loop to check whether a product has the primer tag, and if so, display that as one of the products in the loop. I'm relatively new to Liquid, so I don't know how to combine clauses if that's possible. I've looked up the "Where" clause, but don't entirely understand how it works.
Here is the code as it stands:
<div class="{% if settings.show_collection_sidebar %}desktop-10{% else
%}desktop-12{% endif %} tablet-6 mobile-3" id="bside">
<div id="product-loop">
{% for product in collection.products %}
<div class="product {% if settings.products_per_row == '3' %}
desktop-4{% cycle ' first', '', ' last' %}
{% elsif settings.products_per_row == '4' %}
desktop-3{% cycle ' first', '', '', ' last' %}
{% endif %} tablet-half mobile-half"
id="prod-{{ product.id }}"
data-alpha="{{ product.title }}"
data-price="{{ product.price }}">
{% include 'product-listing' %}
</div>
{% endfor %}
</div>
</div>
How could I filter for the desired results? I've tried {% if product.tags contains 'Primer' %} in quite a few places, but to no avail.
Thanks for your help.

Have you considered using two collections to accomplish what you're after?
If you made a dynamic ("Smart") collection for all your primers (Let's assume the handle for the collection is 'primer'), you can access that collection at any time through Liquid:
{% assign primer_collection = collections['primer'] %}
{% for product in primer_collection.products %}
<h2>HAVE SOME {{ product.title }}!</h2>
{% endfor %}
Then, if you wanted to exclude all of your primer products from the main collection, create a collection named 'all' (or at least with the handle 'all' - the actual title doesn't strictly matter - see footnote). By default, the 'All' collection is, true to its name, every product in your store. However, if you create your own 'all' collection, you can define it to mean "Everything except certain items" - in your case, 'everything but the primer'
Having Shopify pre-filter everything for you through the collections themselves greatly reduces the headaches of trying to apply filters after-the-fact and dealing with misleading item counts, uneven pagination, etc.
Handles: At the bottom of each collection and product in your Shopify admin is the SEO settings. Editing these allows you to change what the handle for the collection/product is, and this is what Shopify uses to look up your collection/product internally

Related

How can I display specific Collection on a product or list page

I have a shopify store with new, used and refurbished amplifiers & speakers.
This condition is stored in a manual collection (four of them). The others collections being automated.
I would like to display the condition of the product on the product page or the products list.
So basically I need to get all the collections and filter to display one of the four :
If the product belongs to collection "used" display collection "used"
If the product belongs to collection "new" display collection "new"
etc...
The closest to what I want to do has been made trough this code :
{% assign product_collection = product.collections.first %}
{% if product_collection %}
This product is part of my {{ product_collection.title | link_to: product_collection.url }} Collection
{% endif %}
Found here : https://community.shopify.com/c/Shopify-Design/RESOLVED-Display-Collection-on-Product-Page/td-p/230899
With this I am not able to filter on the four collections.
I have spent the day on this...If somebody can help, that would save my day :)
You can add all collections(title or handle value) on the product-grid-item.liquid.
<div class= "grid-item
{% for product_collection in product.collections %}
{{product_collection.title | handle }}
{% endfor %}
">
...
</div>
And then you can filter them with JavaScript on the frontend.
Hope this could help you

Unless Clause Leaving Blank Space in Collection Page

I asked about including a where clause in the collections page the other day but decided to scrap going that route. I'm now just using unless logic in the page. This route has worked except for one tiny flaw. When I include the unless clause in the collection.liquid page, the grid still leaves an empty space where the product that's being ignored would normally sit, and also including it in the count. How can I get this space to go away and how can I get it to not include it in the product count? It seems like the unless clause is just keeping it from appearing, not truly excluding it.
Here is the code:
<div class="{% if settings.show_collection_sidebar %}desktop-10{% else
%}desktop-12{% endif %} tablet-6 mobile-3" id="bside">
<div id="product-loop">
{% for product in collection.products %}
{% unless product.title contains "Sampler" %}
<div class="product {% if settings.products_per_row == '3'
%}desktop-4{% cycle ' first', '', ' last' %}{% elsif
settings.products_per_row == '4' %}desktop-3{% cycle ' first',
'', '', ' last' %}{% endif %} tablet-half mobile-half" id="prod-
{{ product.id }}" data-alpha="{{ product.title }}" data-price="
{{ product.price }}">
{% include 'product-listing' %}
</div>
{% endunless %}
{% endfor %}
</div>
</div>
I've tried moving around where the unless code is, outside the for loop, outside the product loop, but nothing has worked.
Thanks,
RDV
What you've written should be working - I would check your product-listing snippet for anything referencing forloop.index, since the index is a variable that will be incremented even if you're not doing anything on that iteration.
For cleanliness in the liquid code, I usually use the continue statement so that I'm not wrapping potentially huge blocks of code with liquid tags - I find that it gets hard enough to keep track of all the openings/closings in liquid if I'm doing moderately complex stuff otherwise. So with this example, instead of using unless you could consider {% if product.title contains 'Sampler' %}{% continue %}{% endif %}
But all of this is ignoring the most powerful tool that we have at our disposal - making sure the collection only contains the right products in the first place!
I'm assuming that you're looking at your "All Products" collection with this code. By default, there's an invisible collection in your store called "All" that, true to its name, contains every product visible in the online store. However, you can override this by making your own collection named 'All' (specifically, with a handle of 'all' - the actual title doesn't matter as long as this is the handle). If you want "All" to mean "Everything that's not a sampler", just make a "Smart Collection" with the condition "Product title DOES NOT CONTAIN 'Sample'" - and voilĂ ! There's nothing to skip and the total count will always be correct!

Shopify Metafields If Statement

For some reason, when I wrap this metafield within an if statement it seems to break, but works perfectly when out of the statement. The product I'm looking at has a rating set within metafields of '3' but still shows 'Not Been Rated Yet' which is really odd!
{% assign review = product.metafields.review %}
{% assign key = 'rating' %}
{% if product.metafields.rating != blank %}
<img src="//cdn.shopify.com/s/files/1/1513/9752/t/3/assets/{{ review.rating }}.svg"/>
<span>Scored {{ review.rating }}/5 with a Verified Tester</span>
{% else %}
<img src="//cdn.shopify.com/s/files/1/1513/9752/t/3/assets/unrated.svg"/>
<span>Not been rated yet. Become a tester!</span>
{% endif %}
Would anyone be able to help with this?
This was fixed due to a name space error. As you're already within a 'product' you don't need to retell Shopify that you want to access the product... field.
Fixed by trying the if statement against 'review.rating != blank'

Multiple linklists on shopify categories (sidebar)

Yet another shopify question. I have a sidebar on shop/collections page
settings.sidebar_categoryblock_linklist
Which shows links to my main collecions: men/woman/kinds ect.
Yet I have another collection list based on product tags:
Winter/Autumn/Casual/Business etc
How do I get to display them on the sidebar? Meaning how can I put several linklists as categories link lists?
Thanks in advance
Julia
In a collection page, you have access to all the tags (unique) that the products have. You can use these tags to see if the belong to any collection and then display them. The code is as follows:
{% for tag in collection.all_tags %}
{% assign c_collection = collections[tag] %} // c_collection is used so the current display page's collection doesn't get disturbed
{% if c_collection.title != '' %}
{{ tag | link_to: c_collection.url,c_collection.title }} // not sure about this, but you can use proper html as well here
{% endif %}
{% endfor %}

Get a list of collections for all products in current Shopify collection

We have a primary collection of t-shirts for the product type of clothing (product type is just in case, so, nobody would bring it up).
And all of them are also in a few other collections like "linen t-shirts", "silk t-shirts" etc.
Let's say i'm on the page of t-shirts collections, and I need to display a list of all the secondary collections like linen, silk and so on. How do I do that?
What I've got so far, is I can get a list of collections for each individual product with following code. Further, I'd pull them and sort with js. But I was really hoping for an easier way to get the list.
Here's the liquid code I'm using for each product's collection list:
{% for product in collection.products %}
{% for collection in product.collections %}
<div>{{collection.title}}</div>
{% endfor %}
{% endfor %}
p.s.
variants are not an option neither
On collection page, fetch all the collections. If your current collection handle is t-shirt and your sub collection handle is linen-t-shirt. Check for all the collections which contains current collection handle, then show the details.
{% assign currentCol = collection.handle %}
{% for collection in collections %}
{% if collection.handle contains currentCol %}
{{ collection.title }}
// further code
{% endif %}
{% endfor %}