Skip to content

Instantly share code, notes, and snippets.

@martinsoender
Forked from davidemaser/shopifyCartQueue.js
Created July 5, 2021 18:40
Show Gist options
  • Save martinsoender/befb1d6a6b25695c50063ac0546ee442 to your computer and use it in GitHub Desktop.
Save martinsoender/befb1d6a6b25695c50063ac0546ee442 to your computer and use it in GitHub Desktop.
Using the Shopify queue to add items to the cart
Shopify.queue = [];
Shopify.moveAlong = function() {
if (Shopify.queue.length) {
var request = Shopify.queue.shift();
Shopify.addItem(request.variantId, request.quantity, request.properties, Shopify.moveAlong);
}
else {
document.location.href = '/cart';
}
};
Shopify.addItem = function(id, qty, properties, callback) {
var params = {
quantity: qty,
id: id
};
if(properties != false){
params.properties = properties;
}
$.ajax({
type: 'POST',
url: '/cart/add.js',
dataType: 'json',
data: params,
success: function(){
if(typeof callback === 'function'){
callback();
}
},
error: function(){}
});
}
function push_to_queue(variantID, quantity, properties,callback) {
Shopify.queue.push({
variantId: variantID,
quantity: quantity,
properties: properties
});
if(typeof callback === 'function'){
callback();
}
}
push_to_queue(1234, 1, {}, Shopify.moveAlong);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment