Last active
May 5, 2022 06:14
-
-
Save carolineschnapp/18c5a74eeb953c7e3d92 to your computer and use it in GitHub Desktop.
Disable sold-out variants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% comment %} | |
Disable 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:eq({{ forloop.index0 }})').prop('disabled', true); | |
{% endunless %} | |
{% endfor %} | |
jQuery('.single-option-selector').trigger('change'); | |
observer.disconnect(); | |
}); | |
observer.observe($addToCartForm[0], config); | |
} | |
</script> | |
{% endif %} |
@aman-developer Thought would be easier... Many people asking for it on Internet and poor options... Thanks!
@rubenkss Just have need changes few elements like classes .. etc.
@aman-developer Count on you? ;)
hi. has a solution been found to the problem?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rubenkss I can do. Lot of changes required in theme files along with linked-options snippet.