Skip to content

Instantly share code, notes, and snippets.

@gopperman
Created May 16, 2014 01:09
Show Gist options
  • Save gopperman/304977be418f34e968ca to your computer and use it in GitHub Desktop.
Save gopperman/304977be418f34e968ca to your computer and use it in GitHub Desktop.
Javascript for a quick cart view / add in shopify
var Shopify = Shopify || {};
jQuery(function($) {
Shopify.shop = "redacted.url";
Shopify.onCartUpdate = function(cart) {
$.getJSON('/cart.js', function (cart, textStatus) {
$('.cartcount').each(function() {
$(this).html(cart.item_count);
});
if (cart.item_count == 0) {
console.log('if');
$('#cart').fadeOut('500', function() {
$('#empty').fadeIn('500');
});
}
var subtotal = cart.total_price.toString();
$('#cart-subtotal').html('<b><span class="money">$'+ subtotal.slice(0,-2) +'.' + subtotal.slice(-2)+'</span></b>');
});
Shopify.loadCart();
};
Shopify.quickAddtoCart = function(form_id) {
$.ajax({
type: 'POST',
url: '/cart/add.js',
dataType: 'json',
data: $('#'+form_id).serialize(),
success: Shopify.addToCartOk,
error: Shopify.addToCartFail
});
};
Shopify.addToCartOk = function(product) {
$('#status-msg .msg').html('Item Successfully added to your cart');
$('#status-msg').stop(true, true).fadeIn('400').delay('3000').fadeOut('400');
Shopify.onCartUpdate();
};
Shopify.addToCartFail = function(product) {
$('#status-msg .msg').html('Oops! We have a problem. This might be out of stock, or already in your cart');
$('#status-msg').stop(true, true).fadeIn('400').delay('4000').fadeOut('400');
};
Shopify.loadCart = function() {
$('#cart-qv').load('/cart?view=quick', function(){
$('#cart-qv .cart-item .x').on('click', function() {
$('.cart-item[data-id="'+$(this).data('id')+'"]').animate({height: 'toggle', opacity: 0}, 400, function() {
Shopify.removeItem($(this).data('id'));
$(this).remove();
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment