Skip to content

Instantly share code, notes, and snippets.

@darryn
Created September 18, 2014 21:35
Show Gist options
  • Save darryn/c14cfb2590ac9631e56c to your computer and use it in GitHub Desktop.
Save darryn/c14cfb2590ac9631e56c to your computer and use it in GitHub Desktop.
Lucid One - Variant Image Selector Script
{% if product.available %}
<script>
var selectCallback = function(variant, selector) {
if (variant && variant.available == true) {
if (variant.compare_at_price > variant.price) {
jQuery('#price-field h3').html(Shopify.formatMoney(variant.price, "{{shop.money_with_currency_format}}") + "&nbsp;<span class='{{ settings.sale_icon }}'>{{ settings.sale_text }}</span>&nbsp;<s><small>" + Shopify.formatMoney(variant.compare_at_price, "{{shop.money_with_currency_format}}") + "</small></s>"); // update price field
} else {
jQuery('#price-field h3').html(Shopify.formatMoney(variant.price, "{{shop.money_with_currency_format}}")); // update price field
}
{% if settings.show_currency_converter %}
Currency.convertAll(shopCurrency, jQuery('[name=currencies]').val(), 'form[action="/cart/add"] span.money');
{% endif %}
// selected a valid variant
jQuery('#addtocart').removeClass('disabled').removeAttr('disabled'); // remove unavailable class from add-to-cart button, and re-enable button
jQuery('#addtocart').text('Add to Cart');
} else {
// variant doesn't exist
jQuery('#addtocart').addClass('disabled').attr('disabled', 'disabled').text('Sold Out'); // set add-to-cart button to unavailable class and disable button
}
var new_image_id = {{ featured_image.id }};
if (variant && variant.featured_image) {
var original_image = $(".featured-image img"), new_image = variant.featured_image, new_image_id = variant.featured_image.id;
Shopify.Image.switchImage(new_image, original_image[0], function (new_image_src, original_image, element) {
$(element).parents('a').attr('data-image-id', new_image_id);
$(element).attr('src', new_image_src.replace('_large','_1024x1024'));
});
}
$(".featured-image").click(function(){
$("#"+new_image_id).trigger("click");
});
};
// initialize multi selector for product
jQuery(function() {
{% if product.variants.size > 1 or product.options.size > 1 %}
new Shopify.OptionSelectors("product-select", { product: {{ product | json }}, onVariantSelected: selectCallback, enableHistoryState: true });
{% endif %}
});
</script>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment