All things Shopify and commerce
HI,
I try to add image swatches on my product page. I know about a method where you can connect image/color swatches with meta field connection. But is there an easier way to add it with coding where it will automatic pick the picture selected on the variant?
Hi @Latelier539 I was able to implement something like this on the dawn theme and this would work on any free theme provided by Shopify. Please check reference site below of one of my client.
Site: www.kpsclothing.in
If this is something you are looking for, please reach out via mail for a convenient conversation and collaboration.
Best
Shadab
Hi @Latelier539,
To add image swatches to your product page on the Dawn theme and have them automatically display the selected variant’s image, here’s a streamlined approach using Liquid and JavaScript. This avoids needing to use metafields:
Access Theme Code:
Edit the Product Template:
Add Image Swatches:
{% for variant in product.variants %}
<img src="{{ variant.featured_image.src | img_url: '100x100' }}" alt="{{ variant.title }}" data-variant-id="{{ variant.id }}" class="variant-swatch" />
{% endfor %}
JavaScript for Swatch Selection:
document.querySelectorAll('.variant-swatch').forEach(swatch => {
swatch.addEventListener('click', function() {
const variantId = this.getAttribute('data-variant-id');
const selectedVariantImage = this.getAttribute('src');
document.querySelector('.main-product-image').src=selectedVariantImage;
});
});
This method lets the product image update dynamically based on the selected variant without requiring metafield setup, making it simpler and more responsive for customers.
For further optimization, SEOPro can assist in ensuring your image alt text aligns with SEO best practices, as well as audit overall product page SEO to improve your store’s visibility.
Full disclosure: We are the developers of SEOPro. If you have questions, feel free to reach out at support@seo-pro.app.
Phil
SEOPro Developer
Thank you for the response, sadly i couldn't get it to work.
I couldn't find the locations where i have to add the code. i tried some but it didn't work sadly.
Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024In today’s interview, we sat down with @BSS-Commerce to discuss practical strategies f...
By JasonH Nov 13, 2024