Pulling Products From Multiple Collections - Shopify Liquid - shopify

I am having trouble trying to pull products from more than one collections that are associated with the product on its product page. I have coded the following:
{% for product in product.collections limit: 6 %}
** Products **
{% endfor %}
However, this just pulls all the list of collections that's associated with the product rather than the products in those collections. I have then tried the following:
{% for product in collections[product.collections].products limit: 6 %}
** Products **
{% endfor %}
Which came back with an "Liquid error: Expected handle to be a String but got Array" error message.
I am not sure how I should approach this. Does anybody know where I've gone wrong?

You are going to fast. Collections is an array so you must first define which collection you need before its products.
So, for example if you want only products from first collection of current product you might try this to access the collection itself inside product.collections array:
{% for product in product.collections.first.products %}
Do something with product object.
{% endfor %}
Then, regarding what you are trying to achieve, first loop through product.collections, then loop through each collection products. Like this:
{% for collection in product.collections %}
{% for product in collection.products limit:6 %}
Do something
{% endfor %}
{% endfor %}
Please note that double loops are greedy and might affect page load time. So you might need to limit primary loop too to avoid unnecessary load if product belongs to lot of collections.

Related

Retrieve all products that contains specific handle in shopify

I want to retrieve list of products that have matching handles.
eg :I have four products having specific words similar in handle
2000-kawasaki-1100-stx-ajpc
2000-kawasaki-1100-stx-kjpc
2000-kawasaki-1100-stx-cpjc
2000-kawasaki-1100-stx-dfgc
Now i want to retrive products that contain 2000-kawasaki-1100-stx in handle.
i tried using
{{ all_products['2000-kawasaki-1100-stx'] }}
But this didnt work ,i cant also loop through all collection because of shopify limit.
Is there any way to get this work ?
as you say, the all_products object has a limit of 20 unique handles per page. If you want more than 20 products, then consider using a collection instead.
An example collection iteration:
{% for product in collections.all.products %}
{%- case product.id -%}
{%- when '123456' -%}
{% assign my_product = product %}
{% endcasae %}
{% endfor %}
instead of using "if" please use "case" so best code practice and fast result while searching on loop of around 50 iterations.

Get all shopify products filtered by a Tag using Liquid

Is it possible to query all products in shopify by a tag (or set of tags) using Liquid.
I thought I had it by looping through collections.all.products and then filtering in the loop but then realised that the collection was limited to a page size of 50.
If it meant to be displayed in frontend, Shopify does not support the paginate hack quoted by Hymnz above.
However, even if query is limited to 50 products, you may clearly make your query and then use pagination.Something like this may work to filter products list:
{% for product in collections.all.products %}
{% if product.tags contains 'mytag' %}
Do something
{% 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 %}

Shopify trying to add all products to homepage

I am trying to add all my products to the front page and not only a selection of 3, i'm a major newby with shopify if someone could point me in the right direction that would be great.
{% for product in collections.frontpage.products %}
{% include 'product' with product %}
{% endfor %}
You could try:
{% for product in collections.all.products %}
{% include 'product' with product %}
{% endfor %}
Although it's probably better to alter your frontpage collection to include all products. Use a smart collection with the condition Product price is greater than 0.
If you have more than 50 products you'll need to use pagination.
Here's a similar question: How do I display all of my products on my home page using Shopify and Liquid?.

How to randomize related products in Shopify

I have the standard Shopify theme Minimal. Products are assigned to Collections.
Related Items on each product just show the first 4 items it finds in the related Collections. As there are many items in each collection, a lot of the time there related items are completely the same on 100s of products.
How do I edit the code to randomize the results on Related Products?
Steph's answer is better but there is also this non-javascript (and also not truly random, but I like it anyway) solution that hacks date:
{% assign relatedCollection = collections['related-products'] %}
{% assign index = 'now' | date: '%S' %}
{% assign index = index | times: relatedCollection.products.size %}
{% assign index = index | divided_by: 60 %}
{% for product in relatedCollection.products offset: index %}
...
{% endfor %}
Take a look at this article on the Shopify wiki: Recommend related products to your customers. The section "Find a relevant Collection to recommend products" provides a jQuery script for randomizing the related products shown.
You can output all products from the relevant collection and pick a limited number of products randomly using this jQuery plugin: https://github.com/carolineschnapp/jquery-pick/blob/master/jquery.pick.js
See also: Feature multiple random products on your home page
Check the below code for showing the related products using metafields -
{% if product.metafields.related_metafield != blank %}
{% assign metafieldArr = product.metafields.related_metafield.sku | split : ',' %} {% for singleMeta in metafieldArr %} {% assign prod = all_products[singleMeta] %}
{{ prod.title | escape }}
{% endfor %}
{% endif %}
Check Example - https://stellacove.com/collections/boys