Shopify themes, liquid, logos, and UX
We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more
Hello!
I am using dawn theme. I have products with multiple color variants, and I am hoping to add "# Color Variants" under the title in the product card in gallery view. I would love to add color swatch bubbles as well. I cannot seem to find custom code for this so I thought this would be a great place to start. Thank you!
*I used ChatGPT but it isn't able to match my theme code in the code it provides so it doesn't work properly, and my brain cannot decipher where the errors are.
Hi @passingthrough,
You're using the Dawn theme and want to display "# Color Variants" under the product title in the product card (gallery view). You also want to add color swatch bubbles. Both of these can be added using custom Liquid & CSS code.
Go to Shopify Admin → Online Store → Themes → Actions → Edit Code
Open the Snippets folder and find the card-product.liquid file
Look for the product title code, which should look like this:
<h3 class="card__heading"> <a href="{{ product.url }}">{{ product.title }}</a> </h3>
Right below this, add the following code:
{% assign color_variants = 0 %} {% for option in product.options_with_values %} {% if option.name == 'Color' %} {% assign color_variants = option.values | size %} {% endif %} {% endfor %} {% if color_variants > 0 %} <p class="variant-count">{{ color_variants }} Color Variants</p> {% endif %}
Save the file and check your store! This will display "3 Color Variants" (or however many exist) under the product title.
In the same card-product.liquid file, add the following code right below the previous step’s code:
<div class="color-swatches"> {% for option in product.options_with_values %} {% if option.name == 'Color' %} {% for value in option.values %} <span class="swatch" style="background-color: {{ value | handleize }};"></span> {% endfor %} {% endif %} {% endfor %} </div>
Save the file.
Open the Assets folder and find the base.css file
Scroll to the bottom and add this CSS:
.variant-count { font-size: 14px; color: #555; margin-top: 5px; } .color-swatches { display: flex; gap: 5px; margin-top: 5px; } .swatch { width: 20px; height: 20px; border-radius: 50%; border: 1px solid #ddd; }
Save the file and refresh your store!
With Step 1, no matter where I put the code it does not show. There is no exact code like you referenced so I played around with a few spots that were similar. Is there a way for me to share what I see? Thank you!