Ok let’s make it as simple as i can:
- I have a product let’s call it “Shoes” (its a specific model like: NIke Air Max 90 - just for context)
- I created variants of it with 2 separators Size (A and B) and Colour (1 and 2).
- Shopify created all options A1 A2 B1 B2 - the problem is i have only A1 and B2, so i deleted A2 and B1 from variants list below, and for existing ones i add product pictures.
- Now the fun beggins: On product page when i choose this model (“Shoes”) i see A1 variant displayed, when i want to change Size separator to B - shopify show me B1 (which doesn’t exist in my store - buy options are not avaliable on page)
- Then I have to manualy click to colour 2 option (after that i see B2 variant and buy options are avaliable again)
- Im using Glo Color Swatch - I hide there not avaliable colour variants so in size B page i can only see color 2 indicator but i have to manualy click it to display it after switching size from A1 option), but the problem exist even with this app beeing off - I see all color options but stil have to manualy click it form not existing color variant (color name is crossed) to a avaliable one color variant in this size parameter.
Is there a way to automaticaly switch it to display avaliable color option variant after switching size parameter?
I search the internet and have tried this one and paste it in theme-editor.js but its not working at all:
document.querySelector('[name="Size"]').addEventListener('change', function(e) {
var sizeSelect = e.target;
var size = sizeSelect.value;
var colorSelect = document.querySelector('[name="Color"]');
// Fetch product data
fetch('/products/' + size + '.json')
.then(response => response.json())
.then(data => {
var product = data.product;
// Populate color select options
product.variants.forEach(function(variant) {
// Check if the variant is available and matches the selected size
if (variant.available && variant.option1 === size) {
var option = document.createElement('option');
option.value = variant.id;
option.textContent = variant.option2; // Assuming option2 is color
// Automatically select the first available color
if (!colorSelect.value) {
option.selected = true;
}
colorSelect.appendChild(option);
}
});
});
});