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 %}
5
Answers
I’m not sure how your code looks exactly, since the provided one is only showing the MutationObserver function which handles the select when changed.
Probably you have a product select in your product template where you generate your variants. So you should have something like this in your
product.liquid
file or in the section that is included there:You will have to modify that code so you check if the variable is available. It must become something like this:
So this way your select won’t include any of the unavailable variants beforehand.
In this way, you can remove variants that are sold out.
Thanks
I have removed the out-of-stock and not available variants for a product that has two, or even three options.
Just follow the below-mentioned steps and update your Shopify Theme code:
NOTE:
I recently removed sold-out variants for multiple options. Hope it will work for you too.
Create Snippets with the name of "linked-options".
Paste the code below on your linked-options file.
You can also visit this article that I wrote for reference.
Using javascript when you can use liquid instead is not the best idea.
Go to your main product grid and try this:
You might need to play around with product.available product.variant.available and so on depending on exactly which ones you want to remove, but I can confirm this code works to not render sold out products and at the same time keep all counters/filters/pagination intact.