Shopify themes, liquid, logos, and UX
Hello everyone!
I am pretty new to liquid coding so need a little help.
In the collection page, I want to display a text under a product that says "x colours available". X - being the number of variants the product has.
I was able to add the text under the product that has more than 1 variant using the following code:
{% if product.variants.size > 1 %}
<small> Colors available</small> {% endif %}
Was wondering if there is a small tweak that would allow me to display the number of variants each product has. In other words, if a product has 5 variants, the text underneath would say "5 colours available" and if another product has 2 variants, the text would say "2 colors available".
Hi,
Try to put this code:
{% assign prod_variant_count = 0 %}
{% if product.available and product.variants.size > 1 %}
{% for option in product.options %}
{% if option contains 'Color' %}
{% for variant in product.variants %}
{% assign prod_variant_count = prod_variant_count | plus: 1 %}
{% endfor %}
{% else %}
<small>No Color variants</small>
{% break %}
{% endif %}
{% endfor %}
{% endif %}
{% if prod_variant_count > 0 %}<small>Colors available - {{ prod_variant_count }}</small>{% endif %}
This code will show only variants with this title - 'Color'. Change to another one if you'll need.
Hope it helps!)
Best,
Arthur
Hi Arthur.
I have exactly the same request as Mantas1.
Can you specify exactly where this code should be inserted to display the number of Colors available for each product?
Also, does this apply to the Debut Theme?
Thank you very much!
Kind regards
Mads
In which liquid to put the code in?
This works fine. I have added weight variance. Can you please help me further with dropdown, if i change the value in dropdown, price should change.
Can you specify exactly where this code should be inserted to display the number of Colors available for each product?
Hi,
The above code works file just need to ask if a product option has 5 variants and 1 variant is sold out then how can I show that 4 colors available instead of 5?
Hey, where do you insert the code?
{% assign prod_variant_count = 0 %}
{% if item_featured_product.available
and item_featured_product.variants.size > 0
%}
{% for option in item_featured_product.options %}
{% if option contains 'Size' %}
{% for variant in item_featured_product.variants %}
{% if variant.inventory_quantity > 0 %}
{% assign prod_variant_count = prod_variant_count
| plus: 1
%}
{% endif %}
{% endfor %}
{% else %}
<small>No Sizes variants</small>
{% break %}
{% endif %}
{% endfor %}
{% endif %}
{% if prod_variant_count > 0 -%}
<small>Sizes available - {{ prod_variant_count }}</small>
{%- endif %}
Thank you, that is exactly what I needed!
Hello!
Where did you put this code?
Thanks!
User | RANK |
---|---|
153 | |
127 | |
80 | |
73 | |
66 |
Explore the 30-30-30 rule, a dynamic social media strategy for new businesses. Learn how t...
By Trevor Sep 20, 2023Discover how to leverage the often overlooked footer of your ecommerce site to gain custom...
By Skye Sep 15, 2023In this blog, we’ll be shining a light on Shopify Partners, Experts, and Affiliates. Who a...
By Imogen Sep 13, 2023