Ad

Shopify Ajax API Is Not Accepting Properties

- 1 answer

I am trying to pass three products through the shopify ajax api. It sends over the variant id and quantity but not the properties. the code is below. if I add request.properties to the Shopify.addItem function it stops after one item and gives me a pop saying that one item has been added to the cart. It does not add the other two items nor does it redirect. If I remove request.properties from the Shopify.addItem function it adds all three items to the cart but with no properties.

FINAL CODE Revised from @miglio code

var FreeTrial={
    data:[],
    ini:0,
    total:0,
    addItem:function(qty,id,properties,callback) {
        var params = {quantity:qty,id:id};
        if(properties != false){
            params.properties = properties;
        }
        $.ajax({
            type: 'POST',
            url: '/cart/add.js',
            dataType: 'json',
            async:false,
            data: params,
            success: function(){
                if(typeof callback === 'function'){
                    callback();
                }
            },
            error: function(){}
        });
    },
    recursive:function(){
        FreeTrial.addItem(FreeTrial.data[FreeTrial.ini].qty,FreeTrial.data[FreeTrial.ini].id,FreeTrial.data[FreeTrial.ini].properties,function(){
          //console.log(FreeTrial.data[FreeTrial.ini]);
          FreeTrial.ini += 1;
          if(FreeTrial.ini < FreeTrial.total){
            FreeTrial.recursive();
          }else{
            //return false;
            document.location.href = '/cart';
          }
        });
      },

    begin:function(){
        /* SET YOUR ARRAY QTY's' ID's*/
        FreeTrial.data = [
            {
                "qty": '1',
                "id": 'VARIANT_ID_GOES_HERE',
                "properties": false
            },
            {
                "qty": '1',
                "id": 'VARIANT_ID_GOES_HERE',
                "properties": false
            },
            {
                "qty": '1',
                "id": 'VARIANT_ID_GOES_HERE',
                "properties": false
            },
            {
                "qty": '1',
                "id": 'VARIANT_ID_GOES_HERE',
                "properties": false
            },
            {
                "qty": '1',
                "id": 'VARIANT_ID_GOES_HERE',
                "properties": {
                    "recurring_price": "200.00",
                    "shipping_interval_frequency": "30",
                    "shipping_interval_unit_type": "days",
                    "subscription_id": "12599"
                  }
            }
        ];
        FreeTrial.total = FreeTrial.data.length;
        FreeTrial.recursive();
    }
  }

  FreeTrial.begin();
Ad

Answer

To add properties I use this function and work fine for me.

addItem=function(qty,id,properties,callback) {
  var params = {quantity:qty,id:id};
  if(properties != false){
      params.properties = properties;
  }
  $.ajax({
    type: 'POST',
    url: '/cart/add.js',
    dataType: 'json',
    data: params,
    success: function(){
        if(typeof callback === 'function'){
            callback();
        }
    },
    error: function(){}
  });
}

//Example :
var qty = 1;
var id  = 123456;//variant_id
var properties: {
  "recurring_price": "12",
  "shipping_interval_frequency": "34",
  "shipping_interval_unit_type": "56",
  "subscription_id": "78"
}
//
addItem(qty,id,properties,function(){
    console.log('done');
});

Well, I did this code for multiple items and a save in a gist:
multiple add to cart

Ad
source: stackoverflow.com
Ad