This is what ended up working:
{% for c in product.collections %}
{% if c.handle == "discontinued" %}
This product is Discontinued
{% endif %}
{% endfor %}
Answer from Crystal Groves on Stack OverflowThis is what ended up working:
{% for c in product.collections %}
{% if c.handle == "discontinued" %}
This product is Discontinued
{% endif %}
{% endfor %}
You can create an array of the collections for a product using map on product.collections. This which will create a new array with your specified property, i.e. the handles of each collection.
You can then check if this new array contains the handle you want to work with.
{% assign productCollections = product.collections | map: "handle" %}
{% if productCollections contains 'your-collection-handle' %}
{% comment %} DoSomething {% endcomment %}
{% endif %}
So for your example:
{% assign productCollections = product.collections | map: "handle" %}
{% if productCollections contains 'discontinued' %}
This product is Discontinued
{% endif %}
You can map other fields if your case is different, such as the title.
You have access to the current_tags, refer to docs: https://shopify.dev/docs/themes/liquid/reference/objects/current-tags
This will return an array of all the tags you are viewing at the moment (in case you are viewing more than one tag).
So your check will be:
{% if current_tags contains 'mens' %}
do something
{% endif %}
That's pretty much the just of it.
I really like how the menu is coming along! Here are some ideas you could consider for your category specific menu, if you've not gotten where you want yet.
For your url strategy, what you're looking for is handle. Handles are specific to liquid. https://shopify.dev/docs/themes/liquid/reference/basics/handle
You could make a custom collection template if those 2 categories need to be fairly different: https://shopify.dev/tutorials/customize-theme-create-alternate-templates. If you do that, then you can use
template_prefixfrom the collection object.Assign a variable outside your loop and then set it inside the loop like:
{% assign is_mens = false %} {% for tag in product.tags %} {% if tag contains 'mens' %} {% assign is_mens = true %} {% endif %} {% endfor %}
then {% if is_mens %} or {% unless is_mens %} for your dynamic content, or a case statement to define content specific to categories in your menu.
Hope this helps!
In order to get the CURRENT collection, you MUST have the collection handle in your URL.
So for example if the product URL is something like so /collections/COLLECTION_HANDLE/products/PRODUCT_HANDLE you will have access to the current collection.
In your case since you don't have access to the collection title I assume that your URL is just /products/PRODUCT_HANDLE.
This means that you are generating the wrong URL's ( not wrong, but not full ). You must look for product.url in your collection and add the filter within: collection.
So your collection liquid code should look something like this
{% for product in collection.products %}
... SOME OUTPUT ...
<a href="{{ product.url | within: collection }}">Details</a>
{% endfor %}
This will force your product url to include the collection URL as well.
Try it if your URL in this format "/collections/COLLECTION_HANDLE/products/PRODUCT_HANDLE"
Try This:
This product is in the {{ collection.title }} Collection.
I am using BeYours Theme from Shopify Theme Store.
I was able to edit the theme code to only count available products when showing the count
Screenshot
Used the following code for the count:
{% for product in collection.products %}
{% if product.available %}
{% assign available_products = available_products | plus: 1 %}
{% endif %}{% endfor %}
This works fine on the collection page but on the Search Results page, it shows the correct count if the result count is less than 10. If the results are more than 10, it will show "10".
I am not able to fix this count. Here's the code for search template:
{% for product in search.results %}
{% if product.available %}
{% assign available_products = available_products | plus: 1 %}
{% endif %}{% endfor %}
How can I get the correct Search results count?
The object product.collections is an array containing all collections where the product is listed.
So you may use this:
{% if product.collections.size < 1 %}
Do something
{% endif %}
Documentation about arrays here: https://shopify.dev/api/liquid/filters#size
{% if 0 == product.collections | size %}
!! put your code here !!
{% endif %}
Or
{% for _ in product.collections %}
{% else %}
!! put your code here !!
{% endif %}