Created
January 22, 2017 00:39
-
-
Save seedcms/2e5f05c32aacbefc27bc846e18ddc501 to your computer and use it in GitHub Desktop.
Add to Cart JS
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
| (function(){ | |
| $('.add_to_cart').on('click', function(e){ | |
| var variantId = VARIANTID GOES HERE; | |
| jQuery.getJSON('/cart.js', function(data){ | |
| var items = data.items; | |
| console.log(items); | |
| var currentVarId; | |
| var quantity; | |
| var isProduct = false; | |
| items.forEach(function(e,i,a){ | |
| console.log(e); | |
| currentVarId = e.variant_id; | |
| if(currentVarId === variantId) { | |
| isProduct = true; | |
| quantity = e.quantity; | |
| } | |
| }); | |
| if(!isProduct) { | |
| jQuery.post('/cart/add.js', { | |
| quantity: 1, | |
| id: variantId | |
| }); | |
| } else { | |
| if(quantity > 1) { | |
| jQuery.post('/cart/change.js', { | |
| quantity: 1, | |
| id: variantId | |
| }); | |
| } | |
| } | |
| }); | |
| }); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment