Skip to content

Instantly share code, notes, and snippets.

@kinge
Created March 16, 2015 16:09
Show Gist options
  • Save kinge/68e27d87d1a2fe9e8d8e to your computer and use it in GitHub Desktop.
Save kinge/68e27d87d1a2fe9e8d8e to your computer and use it in GitHub Desktop.
<script>
var length = {{ product.variants.size }};
$(document).ready(function () {
$("#{{ product.id }}-quantity-0").focus();
$("#submit-table-{{ product.id }}").click(function(e) {
e.preventDefault();
//array for Variant Titles
var toAdd = new Array();
var qty ;
for(i=0; i < length; i++){
toAdd.push({
variant_id: $("#{{ product.id }}-variant-"+i).val(),
quantity_id: $("#{{ product.id }}-quantity-"+i).val() || 0
});
}
function moveAlong(){
if (toAdd.length) {
var request = toAdd.shift();
var tempId= request.variant_id;
var tempQty = request.quantity_id;
var params = {
type: 'POST',
url: '/cart/add.js',
data: 'quantity='+tempQty+'&id='+tempId,
dataType: 'json',
success: function(line_item) {
//console.log("success!");
moveAlong();
},
error: function() {
//console.log("fail");
moveAlong();
}
};
$.ajax(params);
}
else {
document.location.href = '/cart';
}
};
moveAlong();
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment