Skip to content

Instantly share code, notes, and snippets.

@nilocortex
Created June 16, 2020 20:26
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 nilocortex/285b1056ac1b21091dbd1e546e70e9c1 to your computer and use it in GitHub Desktop.
Save nilocortex/285b1056ac1b21091dbd1e546e70e9c1 to your computer and use it in GitHub Desktop.
Add Variant ID Url switching
$container.on(theme.Variants.eventStrs.onVariantChanged, function (e, variant) {
if (variant) {
if(window.location.pathname.indexOf('product') >= 0){
var newurl = window.location.protocol + '//' + window.location.host + window.location.pathname + '?variant=' + variant.id;
window.history.replaceState({path: newurl}, '', newurl);
}
self._changeImage($container, variant);
self._changePrice($container, variant);
self._changeSKU($container, variant);
self._updateAvailability($container, variant.available);
$('.shopify-payment-button').show();
self._changeMetafield($container, variant);
} else {
if(window.location.pathname.indexOf('product') >= 0){
var newurl = window.location.protocol + '//' + window.location.host + window.location.pathname;
window.history.replaceState({path: newurl}, '', newurl);
}
$('.shopify-payment-button').hide();
self._changePrice($container, null);
self._changeSKU($container, null);
self._updateAvailability($container, false, '{{ 'products.product.unavailable' | t }}');
}
});
//Set up the listener for when a new quantity is entered
$container.on(theme.Variants.eventStrs.onQtyChanged, function (e, newQuantity) {
var $selection = theme.Variants.getSelectedVariant($container);
self._updatePrice($container, $selection.data('base-price'), $selection.data('compare-price'), newQuantity);
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment