-
-
Save martinsoender/befb1d6a6b25695c50063ac0546ee442 to your computer and use it in GitHub Desktop.
Using the Shopify queue to add items to the cart
This file contains hidden or 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
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