Last active
October 8, 2018 18:55
-
-
Save cpres/cdbcf840201175737bae to your computer and use it in GitHub Desktop.
Robust Shopify Cart JS Methods
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
__init__: function (p) { | |
var self = this; | |
self.initialConfig = p; | |
Shopify.queue = []; | |
}, | |
addItem: function(prod,num, attr) { | |
var num = (num) ? num : 1; | |
this.pushQueue(prod,num,attr); | |
}, | |
removeItem: function(prod,attr) { | |
this.pushQueue(prod,0,attr); | |
}, | |
pushQueue: function(id,n,attr) { | |
Shopify.queue.push( { | |
variantId: id, | |
quantity: n, | |
attributes: attr | |
} ); | |
this.moveAlong(); | |
}, | |
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(); | |
if (request.attributes) { | |
Shopify.updateCartAttributes(request.attributes,function(){}) | |
} | |
if (request.quantity > 0) { | |
Shopify.addItem(request.variantId, request.quantity, this.moveAlong); | |
} else { | |
Shopify.removeItem(request.variantId, this.moveAlong); | |
} | |
} | |
// If the queue is empty, we will redirect to the cart page. | |
else { | |
document.location.href = '/cart'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
VC.addItem({{id}},1,{'gift-box':true});
or
VC.removeItem({{id}},{'gift-box':''});