Last active
October 11, 2019 19:38
-
-
Save drabbytux/d1444d3709caf83144023e357ff2ce02 to your computer and use it in GitHub Desktop.
Remove variables from single variant selector on product pages that are sold out
This file contains hidden or 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 %}Place this at the bottom of the section/product.template.liquid file{% endcomment %} | |
{% if product.options.size == 1 %} | |
<script> | |
var product_variants_removed = [ | |
{%- for variant in product.variants -%} | |
{%- unless variant.available -%} | |
'{{ variant.title }}', | |
{%- endunless -%} | |
{%- endfor -%} | |
]; | |
</script> | |
{% endif %} |
This file contains hidden or 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
/* ALL OTHER THEMES - Place this at the bottom of assets/theme.js */ | |
$( document ).ready(function() { | |
if( typeof product_variants_removed != undefined ) { // was there items to be removed? | |
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() { | |
product_variants_removed.forEach(function(item){ | |
$('.single-option-selector option').filter(function() { return $(this).text() === item; }).remove(); | |
}); | |
observer.disconnect(); | |
}); | |
observer.observe($addToCartForm[0], config); | |
$('.single-option-selector').trigger('change'); | |
} | |
} | |
}); |
This file contains hidden or 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
/* BROOKLYN THEME ONLY - Place this at the bottom of assets/theme.js */ | |
$( document ).ready(function() { | |
$('single-option-selector__radio').trigger('change'); | |
if( typeof product_variants_removed != undefined ) { // was there items to be removed? | |
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() { | |
product_variants_removed.forEach(function(item){ | |
$('#ProductSelect-option-size-'+item).remove(); | |
$('label[for=ProductSelect-option-size-'+item+']').remove(); | |
}); | |
observer.disconnect(); | |
}); | |
observer.observe($addToCartForm[0], config); | |
$('.single-option-selector__radio').trigger('change'); | |
} | |
} | |
}); |
This file contains hidden or 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
/* NARRATIVE THEME ONLY - Place this at the bottom of custom.js */ | |
$( document ).ready(function() { | |
if( typeof product_variants_removed != undefined ) { // was there items to be removed? | |
var $addToCartForm = $('form[action="/cart/add"]'); | |
if (window.MutationObserver && $addToCartForm.length) { | |
var config = { childList: true, subtree: true }; | |
product_variants_removed.forEach(function(item){ | |
$('.single-option-selector option').filter(function() { return $(this).text() === item; }).remove(); | |
}); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Testing phase 1: