Skip to content

Instantly share code, notes, and snippets.

@mig1098
Last active February 4, 2019 13:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mig1098/1bd84583e73e2ff737c9eece25738939 to your computer and use it in GitHub Desktop.
Save mig1098/1bd84583e73e2ff737c9eece25738939 to your computer and use it in GitHub Desktop.
for add and update items; for delete use update with zero.
var MGUtil={
data:[],
ini:0,
total:0,
action:'add',
reset:function(){
this.ini=0,this.data=[];
},
ajaxpost:function(uri,params,callback){
$.ajax({
type: 'POST',
url : uri,
dataType: 'json',
async:false,
data : params,
success: function(){
if(typeof callback === 'function'){
callback();
}
},
error: function(){}
});
},
addItem:function(qty,id,properties,callback) {
var params = {quantity:qty,id:id};
if(properties != false){
params.properties = properties;
}
MGUtil.ajaxpost('/cart/add.js',params,callback);
},
updateItem:function(qty,id,properties,callback){
var params = {updates: {id: qty}};
if(properties != false){
params.properties = properties;
}
MGUtil.ajaxpost('/cart/update.js',params,callback);
},
recursive:function(){
var actionname = (MGUtil.action == 'update') ? 'updateItem' : 'addItem' ;
MGUtil[actionname](MGUtil.data[MGUtil.ini].qty,MGUtil.data[MGUtil.ini].id,MGUtil.data[MGUtil.ini].properties,function(){
MGUtil.ini += 1;
if(MGUtil.ini < MGUtil.total){
MGUtil.recursive();
}else{
return false;
//document.location.href = '/cart';//AFTER TO ADD ITEMS, GO TO THE CART
}
});
},
begin:function(){
/*SAMPLE*/
/* SET YOUR ARRAY QTY's' ID's PROPERTIES(FALSE IF IS EMPTY)*/
/* SET Action : 'add' , 'update' for performance */
/* For delete use update with 0 for any item */
MGUtil.data = [
{"id":"12345","qty":2,"properties":{"data1":"1"}},
{"id":"34567","qty":3,"properties":{"data2":"1"}},
{"id":"67892","qty":1,"properties":{"data3":"1"}},
{"id":"23456","qty":6,"properties":{"data4":"1"}}
];
MGUtil.total = MGUtil.data.length;
MGUtil.action = 'add';
MGUtil.recursive();
}
}
//
MGUtil.begin();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment