Hi - I have a product this is offered in every letter of the alphabet - so it has 26 variants. For some reason all of the photos for every single variant show up, even when you have the specific variant selected. The product is listed as a draft in my products - but there is a screenshot below.
Does anyone know of any code to add to show only the relevant images/the ones of the selected variant? Thank you! Store is peridotfinejewelry.com
To show only the relevant images for each variant of your product, you will need to modify the code for your product page. You can add some JavaScript to show/hide the appropriate images based on the variant that is selected.
Here is an example of how you can achieve this:
First, add a unique class to each variant’s image element. For example, you can add a class like variant-image-A to the image element for variant A.
Then, add some JavaScript code that will show/hide the appropriate images based on the variant that is selected. You can use the change event listener to detect when a new variant is selected, and then show/hide the relevant images accordingly.
Here’s an example of the JavaScript code:
// Wait for the page to load
document.addEventListener('DOMContentLoaded', function() {
// Get all the variant image elements
var variantImages = document.querySelectorAll('.variant-image');
// Hide all the variant images by default
variantImages.forEach(function(image) {
image.style.display = 'none';
});
// Show the variant image for the selected variant
var variantSelect = document.getElementById('variant-select');
variantSelect.addEventListener('change', function() {
var selectedVariant = variantSelect.value;
var variantImage = document.querySelector('.variant-image-' + selectedVariant);
variantImage.style.display = 'block';
});
});
this example, the code assumes that each variant image element has a class of variant-image followed by a unique class for that variant (e.g. variant-image-A). The code first hides all the variant images by default, and then adds a change event listener to the variant select element.
You will need to modify the class names and selectors to match your specific product page code.
Hi! I made the product live so you can see it and shouldn’t get the error code anymore. I added this as specified - with the meta field and then the if condition to my product template page, but it still doesn’t appear to be working. Can you help?
Thanks! Here is a photo of how I put the condition in:
This solution using alt tags would put your site in violation of the Americans with Disabilities Act (ADA) which requires that alt tags have useful information about the image that cannot be seen by a blind person… Still looking for a solution. Quite frankly, I don’t understand why Shopify has not addressed this problem. It seems pretty critical to me. It is bad UX/UI for any shopper.
What you can do is that keep the alt text whichever you want but additionaly you should put - COLOR_NAME or VARIANT_NAME and rest of the things you should follow from the above video. Bling man will able to know what that image is about.