After quickly searching the internet trying to find code that would display available sizes for clothing products on the product grid (collection page), I found this piece of code:
{% if collection.template_suffix == "hoodies" %}
{% for option in product.options %}
{% if option == 'Size' %}
{% if product.options_with_values[0].name=="Size" %}
{% for variant in product.variants %}
{{ variant.option1 }}
{% endfor%}
{% elsif product.options_with_values[1].name=="Size" %}
{% for variant in product.variants %}
{{ variant.option2 }}
{% endfor%}
{% else %}
{% for variant in product.variants %}
{{ variant.option3 }}
{% endfor%}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
Inserting this into the end of snippets/product-card-grid.liquid, the collection page looks something like this:
As you can tell, it is a little bit clunky that the sizes are way below the actual product. Is there a way I am able to move the sizes in between the product title and the price?
P.S. When I found the code to add the sizes, someone mentioned moving the code relative to other code in product-card-grid.liquid.I was able to move the sizes on top of the product title but not between the two. I do not remember what I did to do so. Here is the entirety of the code in product-card-grid.liquid:
@namphan Thank you so much! It worked and it looks great.
For anyone visiting this thread in the future, replace all the code in snippets/product-card-grid.liquid with the imbedded code in the message above. Then add the code at the end of the message into the end of Assets/theme.css.
I have one more question: How would i apply this to several collections? This code relies on and if statement, mainly this line:
{% if collection.template_suffix == "hoodies"%}
How would I be able to apply this to several collections without copy and pasting this piece of code four times (which does not end up working anyway)?
template_suffix is only supported when you use a collection with a different layout, if you only need to check the collection you can use collection.handle. Refer https://shopify.dev/docs/themes/liquid/reference/objects/collection#collection-handle
If you want to display for multiple collections you can use or:
{% if collection.handle == “hoodies” or collection.handle == “hoodies1” or collection.handle == “hoodies2” %}
Hope it clear to you.
@namphan ,
Thank you! This makes sense. I have put in the code and noticed that the product sizes show on the two pages “Hoodies” and “collectionexample1”, while they do not show up for “tshirts” and “collectionexample2”. I believe the reason is that “hoodies” and “tshirts” have the same page layout, and “collectionexample1” and “collectionexample2” have the same page layout, something you may have mentioned. Is there a way to fix this? (I know I could also just add a “ghost” section at the end of the page to make the layouts different, but I’m looking for clean solutions)
An example of my code:
{% if collection.handle == "hoodies" or collection.handle == "tshirts" or collection.handle == "examplecollection1" or collection.handle == "examplecollection2"%}
Each layout will have its own template suffix, you just need to go to the template file and add the code for each collection.
This means you have only added the code for the suffix template of the collection “Hoodies” and “collectionexample1”, you have not yet added the code for the suffix template of the collection “tshirts” and “collectionexample2”.
Oh, so what I am supposed to do is one by one go to each collection.example.liquid (I only have 4) and add the necessary code from product-card-grid.liquid? The reason I am asking this is that the code in collection.hoodies.liquid and collection.hoodies.liquid are almost identical. The code in collection.collectionexample1.liquid and collection.collectionexample2.liquid are also identical.
Here is what collection.hoodies.liquid and collection.hoodies.liquid looks like:
{% if collection.handle == “hoodies” or collection.handle == “t-shirts” or collection.handle == “living-in-the-wrong-time” or collection.handle == “4x-collection” %}