Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveosoule/95ef3266f37b825d851e7cd3552e71f5 to your computer and use it in GitHub Desktop.
Save steveosoule/95ef3266f37b825d851e7cd3552e71f5 to your computer and use it in GitHub Desktop.
Miva - Get Variant Info on Variant Change
// Copied from @RubenMarin's implimentation
// Add to existing cornerstoneUX.sharedFunctions object...
cornerstoneUX.sharedFunctions.getVariantInfoOnChangegetVariantInfoOnChange = function(fn) {
if (AttributeMachine instanceof Object) { //we first check if AttributeMachine is available
MivaEvents.SubscribeToEvent('variant_changed', function(product_data) {
let self = this;
setTimeout(function() {
let variantId = product_data.variant_id;
let productId = product_data.product_code;
let attribute = {};
if ('attribute' in window && ('product_id' in window.attribute)) {
productId = window.attribute.product_id;
attribute = window.attribute;
}
$.ajax({
url: '/custom-ajax.html',
type: 'POST',
dataType: 'json',
data: {
prodid: productId,
variantid: variantId,
fetch: 'variantinfo'
},
})
.done(function(data) {
if (data instanceof Array) {
fn.call(this, data[0], attribute);
}
});
}, 150);
});
} else {
console.info('AttributeMachine was not found');
}
};
// Then on PROD...
cornerstoneUX.sharedFunctions.getVariantInfoOnChange(function(data, attributes) {
if (data instanceof Object) {
$('.product-information--purchase').find('h1').html(data.name);
$('.product-information--purchase').find('.is-product-code').html(data.code);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment