I'm working on a shoe store website that based on Shopify platform. We want to hide the variants of shoe sizes that are out of stock. After doing some digging I found a Shopify tutorial that tells me to enter a new "snippet" and then edit the theme.liquid file. So I did. This is a single variant case where only shoe sizes vary.
However, when a customer first comes to a product page and clicks a dropdown menu for a shoe size, all of the shoe sizes show up. Unless the customer clicks on the size that is not available and then again clicks on the drop down menu, the menu still displays all of the sizes.
I would like to adjust the code so that the unavailable shoe sizes are eliminated from the beginning. Here is the code snippet:
{% comment %} Remove sold-out variants. Only works for products that have one option. It won't work with products that have two or three options, like Size and Color. See: https://docs.myshopify.io/themes/customization/products/hide-variants- that-are-sold-out {% endcomment %}
{% if product.options.size == 1 %}
<script>
var $addToCartForm = $('form[action="/cart/add"]');
if (window.MutationObserver && $addToCartForm.length) {
if (typeof observer === 'object' && typeof observer.disconnect ===
'function') {
observer.disconnect();
}
var config = { childList: true, subtree: true };
var observer = new MutationObserver(function() {
{% for variant in product.variants %}
{% unless variant.available %}
jQuery('.single-option-selector option').filter(function() {
return jQuery(this).text() === {{ variant.title | json }};
}).remove();
{% endunless %}
{% endfor %}
jQuery('.single-option-selector').trigger('change');
observer.disconnect();
});
observer.observe($addToCartForm[0], config);
}
</script>
{% endif %}