Related Products Shopify Liquid - syntax

I'm trying to determine the correct Shopify Liquid syntax for outputting a list of products that match the same tag as the current product.
This is to appear in a "Related Products" box on the product page, and I'd like it only to list other products that match the same tag of the current product page.
Unfortunately the Related Products wiki page didn't help me with this.

I'm not sure you can get a set of all products with a common tag (although I may be wrong) but here's a possible alternative way to approach it - create a smart collection of products that contain that tag, then output the products from that collection in the related items area.
To connect the product tag to the right collection on the product page, make sure that your collection handle is the same as the tag you're using, then do something like this to grab the right collection based on the tag.
{% for c in collections %}
{% assign t = {{product.tags[0] | handleize}} %}
{% if c.handle == t %}
{% assign collection = c %}
{% endif %}
{% endfor %}
Then just output the products in the collection using the approach outlined in the wiki article you linked.
Something like this (assuming you use a "product-loop" include approach) should do the trick:
{% assign current_product = product %}
{% assign current_product_found = false %}
{% for product in collection.products %}
{% if product.handle == current_product.handle %}
{% assign current_product_found = true %}
{% else %}
{% unless current_product_found == false and forloop.last %}
{% include 'product-loop' with collection.handle %}
{% endunless %}
{% endif %}
{% endfor %}

Related

LIQUID : Trying to NOT include the Variant Images from product thumbnail

'''
{% for media in product.media %}
{% unless media == featured_media %}
{% unless media == variant.featured_media %}
blah blah
{% endunless %}
{% endunless %}
{% endfor %}
'''
Hi. I'm just trying to NOT include the variant images from the thumbnail on my product page.
do I have to assign "variant.featured_media" ?
{%- assign variant.featured_media = variant.featured_media | default: variant.featured_media -%}
I did try to add this on the top of the code but it didn't work.
could anyone please help to see why this is not working ? thank you so much enter image description here

Making a custom collection in shopify liquid - liquid

I want to make a custom collection in collection.liquid based on some conditional scenario and for this I applied concat, append and join but the problem is that it returns ProductDropProductDropProductDropProductDrop... or LazyProductDropCollectionLazyProductDropCollection instead of products. Following is the code snippet
{% assign custom_products = '' %}
{% for product in collections["paneer-easy-indie-bowls"].products %}
{% assign custom_products = custom_products | append: product %}
{% endfor %}
instead of append I tried both join and concat but returns ProductDropProductDropProductDropProductDrop...
{% assign custom_products = custom_products | concat: product %}
then I tried the following:
{% capture custom_products %}
{% for product in collections["paneer-easy-indie-bowls"].products %}
{{ custom_products }},{{ product.handle }}
{% endfor %}
{% endcapture %}
{% assign custom_products = custom_products | split: ',' %}
{% for product in custom_products %}
{{ product}}
{% endfor %}
but this code not appending the products in right way. I want products like same as{{collection.products}}. Any suggestion ?
If I read your pseudo-code correctly, you are trying to build a collection of products from a collection of products. Which leads to the question, why? Since you already have a perfect collection, use it as is!

Access all products for a particular collection in collection.liquid - liquid

I have some header menus as main menu which have other child menus(submenus). I am sufficiently accessing all products collection individually for a particular submenu selection but I want to access all products for a particular collection if "All" (submenu in each menu) is clicked.
Structure is as following:
-Seasonings (Menu)
-- All (submenu - which is to display all products from this collection)
-- Pure seasonings (submenu)
-- Signature seasonings (submenu)
-- Whole seasonings(submenu)
-Drawings
--All (submenu - which is to display all products from this collection)
--Monthly (submenu)
--Annually (submenu)
I am writing like this in header.liquid
{% if link.url == '/collections/seasonings' %}
{% for product in link.object.products %}
{{ product }}
{% endfor %}
{% endif %}
but this is not seems okay.
thanks in advance....
To be able to access an object from links array, you must have used the link selector in Shopify nav admin and not paste directly a link in text input (fig below).
There is a simple way to check that with your code :
{% for link in linklists['my-nav'].links %}
{% if link.type == 'collection' %}
Collection object is available (via link.object)
{% elsif link.type == 'http_link' %}
Link has been pasted and collection object is not available/recognized.
{% endif %}
{% endfor %}

Create a filter list of items where a filter item appears only if there is a story for the product

I'm trying to figure out how to create a filter list where it grabs the product title from a _products directory and it will show the title in the list only if there is a story with the title of the product.
I have a _products list with 11 items and I can show them all, but I want to remove an item from the list of filter items if it's title != a element in the front matter of a list of stories in a separate directory.
{% assign product = site.products %}
{% for product in site.products %}
{% for success in site.success-stories %}
{% if product.title == success.product %}
<li><p class="filter label label-default" data-filter=".{{product.title | downcase}}">{{product.title}}</p></li>
{% endif %}
{% endfor %}
{% endfor %}
What this does is shows only the items that have a matching success-story but if there are multiple stories with the same product in it's front matter it will show that product more than once.
I've got the filtering down, it's just showing the items I can't get down.
Thanks!
I have a feeling you are looking for the contains filter.
Taken directly from the Liquid for Designers wiki:
# array = 1,2,3
{% if array contains 2 %}
array includes 2
{% endif %}
In your case, I'm guessing you want:
{% assign product = site.products %}
{% assign successfulProducts = site.success-stories | map: 'product' %}
{% for product in site.products %}
{% if successfulProducts contains product.title %}
... {{ product }} ...
{% endif %}
{% endfor %}
All I've done here in line 2 is mapping success-stories to successfulProducts. Essentially each story element is replaced by its story.product property.
Then I loop through all the products like you and check each one if it should be displayed. But I don't loop through all the stories - instead I put a simple condition that checks if successfulProducts contains the product title, just as you specified.

How do I show a tag to represent multiple products? Shopify Liquid

Hello and thanks for reading my post!
I have a collection with multiple products. On a custom collection template, I want to show the tags only for those that contain multiple products (or when more than 1 product in that collection have the same tag)
I assume it would go something like:
{% for tag in collection.all_tags %}
{% if tag.product.size >= 1 %}
has more than 1 product.
{% endif %}
{% endfor %}
I've answered similar questions here and here.
You want something like this:
{% for tag in collection.all_tags %}
{% assign products_count = 0 %}
{% for product in collection.products %}
{% if product.tags contains tag %}
{% assign products_count = products_count | plus: 1 %}
{% endif %}
{% endfor %}
{% if products_count > 1 %}
{{ tag }}
{% endif %}
{% endfor %}

Resources