Created
April 10, 2014 17:02
-
-
Save fredryk/10402401 to your computer and use it in GitHub Desktop.
Show percent of savings through the selectCallback
This file contains hidden or 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
| 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