Skip to content

Instantly share code, notes, and snippets.

@fredryk
Created April 10, 2014 17:02
Show Gist options
  • Select an option

  • Save fredryk/10402401 to your computer and use it in GitHub Desktop.

Select an option

Save fredryk/10402401 to your computer and use it in GitHub Desktop.
Show percent of savings through the selectCallback
var selectCallback = function(variant, selector) {
if (variant && variant.available == true) {
// selected a valid variant
var savings = Math.round((variant.compare_at_price - variant.price) / variant.compare_at_price * 100);
console.log(savings);
$('#add-to-cart').removeClass('disabled').removeAttr('disabled'); // remove unavailable class from add-to-cart button, and re-enable button
if(variant.compare_at_price == null){
$('.options .price').html('<strong>'+Shopify.formatMoney(variant.price, "{{shop.money_with_currency_format}}")+'</strong>');
} else {
$('.options .price').html('<strong>'+Shopify.formatMoney(variant.price, "{{shop.money_with_currency_format}}") + '</strong> <span class="compare_at_price">was <del>' + Shopify.formatMoney(variant.compare_at_price, "{{shop.money_with_currency_format}}") + '</del></span> ' + '<span> You save ' + savings + '%</span>');
}
} else {
// variant doesn't exist
$('#add-to-cart').addClass('disabled').attr('disabled', 'disabled'); // set add-to-cart button to unavailable class and disable button
var message = variant ? "Sold Out" : "Unavailable";
$('.options .price').text(message); // update price-field message
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment