Skip to content

Instantly share code, notes, and snippets.

@vvastdev
Last active January 24, 2020 16:14
Show Gist options
  • Save vvastdev/accd534955612605de765d6fd1501af6 to your computer and use it in GitHub Desktop.
Save vvastdev/accd534955612605de765d6fd1501af6 to your computer and use it in GitHub Desktop.
product.js variant availability handling
//called in initVariants
_handleColourAvailability: function(data) {
if(this.variantRadios){
var variants = data.product.variants;
var coloursAvailable = {};
variants.forEach( variant => {
if(variant.available){
coloursAvailable[variant.option1] = true;
}
})
this.variantRadios.forEach( radio => {
var col = radio.dataset.value;
if(!coloursAvailable.hasOwnProperty(col)){
$(radio).css('display', 'none');
}
})
}
},
//called in initVariants and _updateAvailabilty
_handleSizeAvailability: function(data, variantChange) {
var variants = data.product.variants;
var currentColour = variantChange ? variantChange.variant.option1 : data.currentVariant.option1;
var sizeUnavailable = {};
variants.forEach( variant => {
if(!variant.available && variant.option1 === currentColour){
sizeUnavailable["size"] = variant.option2;
}
})
this.sizeRadios.forEach( radio => {
sizeValue = radio.dataset.sizeValue;
sizeUnavailable.size === sizeValue ?
$(radio).addClass('size-unavailable') :
$(radio).removeClass('size-unavailable');
})
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment