Shopify: ajax-cart-template isn't showing tags for all products, It is only showing tags for last product - shopify

I am trying to limit the amount that people can purchase of a product. I am using tags to do this on the cart, collection and product pages. But for some reason it doesn't work on the ajax-cart-template page.
In the {{#items}} I just
{% endraw %}
{{ product.tags }}
{% raw %}
It works fine with one products, but when I add a second it will only show the uppermost products tags. And then repate the tags for the second product with the same tags.

Related

Empty spaces in filtered list

I am trying to return a list of all product IDs which contain 'sometag' in Shopify to power a theme section I'm building.
At first I was stuck by the limit of 50 products in a query (my catalogue is 600+ products), but by adding pagination to my for loop I have been able to return a filtered list from a collection containing all products to show only the tagged products I'm looking for.
However, despite the filtered list usually only containing around 1-5 items depending on tag, Shopify is still displaying the full pagination list for the entire product library - only every page is empty apart from the filtered items (which are usually on page 3 or 4 or something) - so it's not really removing the filtered out items, rather not displaying them and leaving their index spaces there empty.
Does anyone have any advice on how to trim out all the blank entries from the returned list and only display the filtered results?
{% paginate collections['somecollection'].products by 50 %}
{% for product in collections['somecollection'].products %}
{%- if product.tags contains 'sometag' -%}
{{ product.id }}
{% endif %}
{% endfor %}
{{ paginate | default_pagination }}
{% endpaginate %}
EDIT: Shopify tell me this is 'expected behavior' so I guess I need to create some mini app or use JS or something.
Because Liquid pagination happens server-side, it will always be based on the actual size of the collection, not on a sorted subset created for front end display.

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.

Shopify products' collection empty in collection context

I'm trying to filter my related products by collections.
Every product belongs to two collections: One for the material, one for the room.
The Material one is an automatic collection which get products with a certain tag.
The Room one is populated manually.
When I get to the product page I load the related products like this (the related product must share both collections with the current product) :
{% for related_product in collection.products %}
{% if product.collections[0].handle ==
related_product.collections[0].handle and product.collections[1].handle
== related_product.collections[1].handle and related_product.handle !=
product.handle %}
<div class="Carousel__Cell">
{% include 'product-item', product: related_product,
show_product_info:
section.settings.show_product_info, show_labels: true %}
</div>
{% endif %}
{% endfor %}
Strangely this work only for some products. In some of them product.collection seems null, which makes no sense!
All of the products appear properly in the correct collection.
The problem is happening in the context of a collection (but we will need to make it work in all contexts)
We found out it was an import/sync problem. We had to remove all existing products and re-import them. It fixed the issue.

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

Multiple linklists on shopify categories (sidebar)

Yet another shopify question. I have a sidebar on shop/collections page
settings.sidebar_categoryblock_linklist
Which shows links to my main collecions: men/woman/kinds ect.
Yet I have another collection list based on product tags:
Winter/Autumn/Casual/Business etc
How do I get to display them on the sidebar? Meaning how can I put several linklists as categories link lists?
Thanks in advance
Julia
In a collection page, you have access to all the tags (unique) that the products have. You can use these tags to see if the belong to any collection and then display them. The code is as follows:
{% for tag in collection.all_tags %}
{% assign c_collection = collections[tag] %} // c_collection is used so the current display page's collection doesn't get disturbed
{% if c_collection.title != '' %}
{{ tag | link_to: c_collection.url,c_collection.title }} // not sure about this, but you can use proper html as well here
{% endif %}
{% endfor %}