Created
October 20, 2017 23:25
-
-
Save steveosoule/95ef3266f37b825d851e7cd3552e71f5 to your computer and use it in GitHub Desktop.
Miva - Get Variant Info on Variant Change
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
// 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