How did you display a specific product A in a collection, for example?
I had been doing this until just the other day.
{% for product in collection.products %}
{% if product.title contains "Title of product A" %}
{{ product.title }}
{% endif
%}{% endfor %}
I had written code like the following, which retrieves all products and displays the product title only for those that match a specific product name with an if statement.
However, I recently found a Liquid code called all_products and learned that it is possible to retrieve information on a specific product A without turning a for statement. I will share this information with you this time.
Retrieving specific products with Liquid's all_products code
First, the code to retrieve a specific product.
{{ all_products["productA"].title }}
Now the five lines of code, including the for statement written earlier, are reduced to a single line.
By the way, the "priductA" part is the handle of the product.
The "handle" is the last part of the product URL (the following 00 part). https://ec-penguin.
com/products/0000
When multiple products are to be acquired, such as when creating a product list page, it is more efficient to use a "for" statement to acquire many products.
However, when retrieving one specific product, it is overwhelmingly more efficient to use the all_products code.
List of information that can be retrieved from products
By the way, what exactly is meant by product information? You may be wondering what can be obtained.
Here are some typical examples!
{{ all_products["productA"].title }}
The following items can be added after the last period, as in the following example, to obtain the relevant information for a specific product.
-
title
Product title - price Price
of the product - published_at
Date the item was published
tagsAll tags of the product- url
URL of the product
variantsAll variants of the product- handle
handle of the product (the last part of the url )
Note that "all" items such as tags and variants will return multiple values, so a for loop must be used as shown below.
{% for tag in all_products["productA"].tags %}
{{tag}}{% endfor %}
In the case of tags like this, the product will loop for the tags it has, and all tags will be displayed.
There are many more items, so if you want to know all of them, please refer to the official product object document.
https://shopify.dev/docs/themes/liquid/reference/objects/product
Assign specific products to a variable using assign.
And I personally thought it would be easier to use if the specific products obtained by this all_products method were stored in a Liquid variable, so I will share how to do that as well.
Up until now, when acquiring product titles and prices, we have always used the following method.
{{ all_products["productA"].title
}}{{ all_products["productA"].price }}
The same is true of the "I" and "-" statements.
However, writing this all_products part every time is inefficient and makes the code look a little dirty. So, let's store it in the variable productA and use productA.title, etc.
{% assign productA = all_products["productA"]
%}{{ productA.title
}}{{ productA.price }}
Doesn't the code look cleaner this way?
And using this method, it is possible to retrieve multiple products, store them in each variable productA, productB, and use the information.
If you choose "type": "product" in the schema tag
We will conclude with an example of actual use of all_products in a case.
This is an example of utilizing the schema tags that can be edited from the customization screen.
It's a bit difficult and long, so I'll just write it down for my own notes.
What is schema? If you are not sure what you are looking for, please start with this article. Or you can skip this section!
https://ec-penguin.com/blogs/shopify/schema-tag-perfect-guide-steps
In the block of the schema tag, "type": "product" allows me to select a product from a drop-down list.
However, when you select that product, the data passed as data is not the entire product, but only the handle of the product. Therefore
{% assign productA = all_products[block.settings.productA] %}
If you pass the handle retrieved from the block to all_products, as in the following example, you will be able to access all information on the specific product.
However, only 20 products can be retrieved per page. For more information, please refer to the following blog post.
[Shopify] All_products has a limit of 20 products per page, so I'll try to resolve this issue.
How to use Liquid's all_product to get only information on specific products Summary
This is how to retrieve only the information of specific products on Shopify.
It is very useful to be able to use all_products to access all product information if you are able to retrieve only the handle of a specific product.
Shopify is an e-commerce site, so we have to deal with product information properly, so we will be sending out information on how to use the product information little by little.
I'll be sending out information on how to use the products as well. I would appreciate it if you could share it with us on social networking sites, etc.
Have a great day!