Skip to content

Instantly share code, notes, and snippets.

@rolis19
Last active May 14, 2020 18:44
Show Gist options
  • Save rolis19/d0df1c6344b5850bdf9bb65e6068a271 to your computer and use it in GitHub Desktop.
Save rolis19/d0df1c6344b5850bdf9bb65e6068a271 to your computer and use it in GitHub Desktop.
// Using add.js queue
// Use this with assumption that you will not edit or add product after it has ben added to the cart.
$('body').on('click', '.update_desk', function(event) {
event.preventDefault();
var thiss = $(this);
Shopify.queue = [];
//var quantityInCart = parseInt($('#cnt-crt-itm').data("cart-count"));
var productHandle = "."+$(this).attr("data-product-handle");
var loader = $(".loader-"+$(this).attr("data-product-handle"));
loader.css('display', 'block');
jQuery(productHandle).find('.qty_desk').each(function(index){
var id = parseInt($(this).attr('data-variant-id'));
var quantity = parseInt($(this).val());
var lineProp = $(this).attr('data-line-prop');
Shopify.queue.push({
variantId: id,
variantQty: quantity,
variantLine: lineProp,
});
});
Shopify.moveAlong = function() {
// If we still have requests in the queue, let's process the next one.
if (Shopify.queue.length) {
var request = Shopify.queue.shift();
var data = 'id='+ request.variantId+'&quantity='+request.variantQty+'&properties[Order Status]='+request.variantLine;
$.ajax({
type: 'POST',
url: '/cart/add.js',
dataType: 'json',
data: data,
success: function(res){
Shopify.moveAlong();
//quantityInCart += 1;
},
error: function(){
// if it's not last one Move Along else update the cart number with the current quantity
if (Shopify.queue.length){
Shopify.moveAlong()
} else {
//jQuery('.count').html(quantityInCart);
}
}
});
} else {
addToCartOk();
}
};
Shopify.moveAlong();
function addToCartOk(){
jQuery.getJSON('/cart.js', function(cart) {
jQuery('.count').html(cart.item_count);
var number = cart.total_price/100;
jQuery('.totalmoney').html("$"+number.toLocaleString()+", ");
loader.css('display', 'none');
thiss.val("UPDATE");
});
}
});
// We use update.js to update/add multiple product
// Note that update.js can't add/update line properties, but note and attributes can.
$('body').on('click', '.update_desk', function(event) {
event.preventDefault();
// Some identifications
var thiss = $(this);
var productHandle = "."+$(this).attr("data-product-handle");
var loader = $(".loader-"+$(this).attr("data-product-handle"));
var length = jQuery(this).parents('.all-sizes').find('.qty_desk').length;
var data = "";
// Get list of products, put as string.
jQuery(productHandle).find('.qty_desk').each(function(index){
var id = parseInt($(this).attr('data-variant-id'));
var quantity = parseInt($(this).val());
data += "updates["+id+"]="+quantity+"&";
});
// Remove last characte becuase it is '&'
var dataFinal = data.slice(0, data.length-1);
$.ajax({
url: "/cart/update.js",
type: "POST",
data: dataFinal,
dataType: 'json',
beforeSend: function() {
loader.css('display', 'block');
},
error: function(T) {
console.log(T);
},
success: function(data){
console.log(data)
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment