Skip to content

Instantly share code, notes, and snippets.

@resistorsoftware
Created August 27, 2013 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save resistorsoftware/6353325 to your computer and use it in GitHub Desktop.
Save resistorsoftware/6353325 to your computer and use it in GitHub Desktop.
Some simple Ajax cart
<h1>Product.Liquid crap goes here... lalalalala whatever... </h1>
<script type="text/javascript">
// I hacked the Ajax API to add line item properties for a product too.
function addToCart() {
var item = DaveWuzHere.utils.popQueue();
if(item) {
if(item.properties) {
Shopify.api.addItemWithProperties(item.id, item.quantity, item.properties, addToCart);
} else {
Shopify.api.addItem(item.id, item.quantity, addToCart);
}
} else {
// I jump to the cart, but you could do a jig here and make a nice report in a popup or whatever.
document.location.href="/cart"
}
}
jQuery(document).ready(function(){
$("#add_to_cart").on('click', function(e){
e.preventDefault();
// go through a list of checked of radio elements representing different products
var valid = $("input:checkbox[name=main_variant]:checked");
$.each(valid, function(idx,item){
var id = $(this).data("id");
var properties = [];
properties.push("properties[id]="+$(extra).val());
properties.push("properties[title]="+$(extra).data("title"));
DaveWuzHere.utils.pushQueue({id: id, quantity: 1, properties: properties});
});
addToCart();
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment