Get all shopify products filtered by a Tag using Liquid - shopify

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 %}

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.

Pulling Products From Multiple Collections - Shopify Liquid

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.

Show products on article

How can I show a list of products (like a recipe) on a article page.
Can you give me some guidance on what should i use to achieve that behavior?
How can i link those products dynamically to the article?
Getting the products once we know what they are
There are two ways to bring up products on an arbitrary page in Shopify:
1) Using all_products[handle]
Using the product handle to get the product from the all_products global object.
Example:
{% assign ingredient = all_products['lavender-oil'] %}
This works well for small numbers of products, but for large numbers of products it may cause delays in page-loading times. We are also limited to only 20 (I think) calls to all_products per page, so this wouldn't work for recipes with a ton of ingredients in them.
2) Using a collection
Use a collection that contains only the products required. You can reference any arbitrary collection if you know the collection's handle. When making collections, you can also sort products manually to control the order that they appear in when you loop through it. Collections can contain an arbitrary number of products, and I believe the default pagination will give you is either the first 20 or 50 products if you don't specify any other limit. If required you can adjust the number of products served to as high as 1000 by wrapping your collection-product loop with paginate tags (though that upper limit is definitely not recommended for performance reasons)
Example:
{% assign ingredients = collections['love-potion-number-9'] %}
{% for product in ingredients.products %}
<h2>{{ product.name }}!!</h2>
{% endfor %}
The downside for both of these is that you can't write Liquid code inside your article content in Shopify, so this ingredients section would need to be written as a snippet or a section in your theme files and included in your article template used for these recipes.
This leads me to consider the next issue - you would want to include a concept of quantity with the ingredients, and so far neither of the above give us that. So now, the hard part:
Getting that information into a Liquid snippet/section in the first place
There are a few different ways that I can think of offhand that would help you out here. No one is perfect, unfortunately.
Using Metafields
Metafields are a great tool available in Shopify, but unfortunately Shopify doesn't make them easy to use [1].
Once you have a metafield-editing tool, come up with a naming structure for the 'namespace' and 'key' values. For example, you might create the following metafields for the recipe you provided. (Note: How these would be entered will depend on what metafield-editing tool you're using)
namespace: 'ingredients', // We'll use this as the 'box' that holds all our ingredients
key: 'juniper-berry-oil', // This will be the product handle for the product in question
value: '2 drops' // The quantity used for the recipe
namespace: 'ingredients',
key: 'rosemary-ct-camphor-oil',
value: '1 drop'
namespace: 'ingredients',
key: 'cypress-oil',
value: '1 drop'
... (etc) ...
Then, in your theme file where you are creating your ingredient list, you would have code that looks something like this:
{% assign ingredients = article.metafields.ingredients %}
{% for ingredient in ingredients %}
{% assign handle = ingredient.first %}
{% assign amount = ingredient.last %}
{% assign product = all_products[handle] %}
<!-- HTML code here -->
{% endfor %}
Using Tags and Products
If you create a tag-naming scheme, you can loop through those and use them to build your ingredient list. For example, if you give the article a number of tags in the form ingredient_[product-handle]_[amount], you would be able to reference them as:
{% for tag in article.tags %}
{% if tag contains 'ingredient' %}
{% assign breakdown = tag | split: '_' %}
{% assign handle = breakdown[1] %}
{% assign product = all_products[handle] %}
{% assign amount = breakdown | last %}
<!-- HTML Code -->
{% endif %}
{% endfor %}
The downside to this method is that there's no easy way to reorder the tags if done this way - using a collection will give you better control of that.
Getting recipe amounts into a Collection loop
The easiest way to reference a collection would be to have a collection with the same handle as the article - then you can reference the collection and its products as:
{% assign ingredients = collections[article.handle] %}
{% for product in ingredients.products %}
<!-- HTML Code here -->
{% endfor %}
This has the advantage of letting you easily sort the ingredients by setting the collection to have a Manual sorting method, but the corresponding downside is that there's no obvious place to put the quantity information.
One way to get that information in would be to use either tags or metafields - metafields would have the advantage of being able to directly access the quantity for the product - if using the naming convention above in the metafields part of this answer, you could use:
{% assign ingredients = collections[article.handle] %}
{% for product in ingredients.products %}
{% assign amount = article.metafields.ingredients[product.handle] %}
<!-- HTML Code here -->
{% endfor %}
If using tags, you would need a format that could be split up like in the tag section and loop through all your tags each time to find the one for your product. If your tags were set up as ingredient_[product-handle]_[amount]
If using tags, you would need a format that could be split up like in the tag section and loop through all your tags each time to find the one for your product. If your tags were set up as the example above:
{% for tag in article.tags %}
{% if tag contains 'ingredient' and tag contains product.handle %}
{% assign amount = tag | split: '_' | last %}
{% endif %}
<!-- HTML Code -->
{% endfor %}
Hopefully this helps you get going!
[1] Using Metafields: There are several possible solutions for editing metafields in Shopify - my personal preference is the 'Shopify FD' extension for Chrome, but the recent updates to the Shopify admin screens are interfering with this extension's ability to load & show its metafield panels on some pages. I know that product pages still work, but some pages (like collections) don't anymore.
There are also a number of apps available for your store to edit metafields - I haven't used any of them, so I can't speak to their value, but you can view the list here: https://apps.shopify.com/search?q=metafield&st_source=
If you have a coding background, you can also create and update metafields yourself by sending the right data to Shopify's Admin API - see the documentation at https://help.shopify.com/en/api/reference/metafield if you want to try doing it yourself.
Here is a sure to succeed recipe. Write your Article. It will have a handle, unique to it. Save that in your head, clipboard, etc. Now create a manual collection. Give it the same handle. Now you can reference an empty collection by the Liquid:
collection['some-handle-to-an-article']
What if you now filled that collection with the products in your recipe? Eureka. Genial! You can then list them in a simple Liquid for next loop like this:
{% for product in collection['some-handle-to-an-article'] %}
{{ product.title }}
{% endfor %}
Or you could be a smarty pants and gather the handles of the products manually yourself. Store them in a metafield resource assigned to the article. For example, a string like 'a-blah,b-blahblah,c-zigo-von-goober' and then use Liquid to find that metafield in the article template. Split that string by commas. Now use the most excellent all_products like this:
{% assign fizzbuzz = all_products['a-blah'] %}
{{ fizzbuzz.title }}
And there are many more creative options. Shopify Liquid is ripe for play like this. No limits, except on all_products at 20... you cannot go more than that.

Listing all products that contain current tag (on product page)

I've tried searching but couldn't figure this out.
I'm wondering if it's possible to list all products that use the same tag as the current product.
For instance, if I'm on a product page that uses the tag "red", I'd like to list all of the other products that also use that tag.
Is this possible and if so how would I accomplish this?
There are some ways but I'm not sure if these will do you any good.
1) AJAX
The easiest ways is to make an AJAX request to the /collections/all/TAG and get the required products and append them.
2) Paginate hack
{% paginate collection.all.products by 1000 %}
{%- for _product in collection.all.products -%}
{%- if _product.tags contains 'red' -%}
list the product
{%- endif -%}
{%- endfor -%}
{% endpaginate %}
Please note that this may increase the load time drastically depending how many products you have.

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?.