Created
May 20, 2015 01:09
-
-
Save jackmcpickle/11ffa2a50cd7ca711bbf to your computer and use it in GitHub Desktop.
sugar stems site cart js
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
$(function() { | |
var cart_form_options, checkout_form, showSelectedImage, toggleShippingDetails, update_cart, valid; | |
checkout_form = $('.checkout-view').length > 0; | |
if (checkout_form) { | |
$('.js-billing-info').on('change', function(e) { | |
var checked; | |
checked = $(this).is(':checked'); | |
toggleShippingDetails(!checked); | |
}); | |
} | |
toggleShippingDetails = function(toggle) { | |
$('.js-shipping-fields').toggle(toggle); | |
}; | |
showSelectedImage = function() { | |
$('.cart-item .th').each(function(index, element) { | |
var img, selected; | |
selected = $(element).data('selected') - 1 || 0; | |
img = $(element).find('img')[selected]; | |
return $(img).show(); | |
}); | |
}; | |
valid = function(form) { | |
return $(form).valid(); | |
}; | |
$('.js-add-cart-form').on('change', 'select', function(e) { | |
return $(e.delegateTarget).validate(); | |
}); | |
// this will set JSON as the return data type, and identify update_cart as a callback function | |
// post-submit callback | |
// this is the callback script. whenever a form is updated, this script will be called | |
update_cart = function(data, statusText, xhr, $form) { | |
// "data" is the returned data in json format | |
// you can access each of these using data.item_name as shown in the example below | |
if (data.success) { | |
// update the XID hash in the form so we don't run afoul of EE's secure forms | |
$("input[name=XID]").val(data.XID); | |
// using the json data object's data to update various totals. in this case | |
$(".cart-subtotal").html(data.cart_subtotal); | |
$(".cart-items").load('/cart/total_items .totals'); | |
setTimeout(function() { | |
return $($form).find(".js-add-to-cart").text('Done.').addClass('success'); | |
}, 500); | |
setTimeout(function() { | |
return $($form).find(".js-add-to-cart").prop('disabled', false).removeClass('info').text('Add to Cart'); | |
}, 3000); | |
} | |
return true; | |
}; | |
cart_form_options = { | |
success: update_cart, | |
dataType: "json" | |
}; | |
// if any input is changed, the form is sent via ajax. you probably wanna be more selective. | |
$(".js-add-to-cart").on("click", function(e) { | |
var form; | |
e.preventDefault(); | |
form = $(this).closest('form'); | |
if (valid(form)) { | |
$(this).prop('disabled', true); | |
$(this).text('Adding to cart...'); | |
// finding the form that contains this input | |
// initialize the form | |
$(form).ajaxForm(cart_form_options); | |
$(form).submit(); | |
} | |
}); | |
showSelectedImage(); | |
return toggleShippingDetails(!$('.js-billing-info').is(':checked')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment