0

I'm creating an App that feeds with Woo commerce. By now I can show the products list and access to a product , but I'm not able to create an order.

This is the function that creates and POST's the order :

  this.order = function(products, address, tax, total){
    var dfd = $q.defer();
    var clientId = 2;
    var items =[];
    for(var i = 0 ; i<products.length ; i++){
      var item = {product_id: products[i].id,
                  quantity: products[i].qty,
                  price: products[i].price,
                };
      items.push(item);
    }

    $http({
    method: 'POST',
    url: appConfig.DOMAIN_URL + '/wp-json/wc/v1/orders' ,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    paramSerializer: '$httpParamSerializerJQLike',
    params: {
          consumer_key: appConfig.KEY,
          consumer_secret: appConfig.SECRET_KEY,
          line_items: items,
          customer_id: 1,
          total: total,
          status: 'completed',
          shipping: {
              first_name: address.full_name,
              address_1: address.street,
              city: address.city,
              postcode: address.postal_code,
              state: address.state
            },
          shipping_lines: [
            {
              method_id: 'flat_rate',
              method_title: 'Flat Rate',
              total: tax
            }
          ]
        }
    })
    .then(function(res){
      dfd.resolve(res);
    }, function(error){
      dfd.reject(error);
    })
    return dfd.promise;
  }

Response is :

{
    "code": "woocommerce_rest_required_product_reference",
    "message": "Product ID or SKU is required",
    "data": {
        "status": 400
    }
}

Params seems to be there when I inspect with browser :

Name    Value
consumer_key    ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
consumer_secret cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
customer_id 1
line_items[][price] 24490
line_items[][product_id]    27
line_items[][quantity]  1
shipping_lines[][method_id] flat_rate
shipping_lines[][method_title]  Flat Rate
shipping_lines[][total] 0
status  completed
total   24490

I tried hardcoding and using the same data from Woocommerce documentation but the result is the same.

With some combinations throws "product id required" , "quantity required" or "SKU required".

Tried with POSTMAN and order is created with no products.

Any help is appreciated

UPDATE

I tried commenting this line :

paramSerializer: '$httpParamSerializerJQLike',

And now order is created but with no products data, only "non array" params were assigned, so im almost sure is a formatting problem...

Example object :

var data = {
  payment_method: 'bacs',
  payment_method_title: 'Direct Bank Transfer',
  set_paid: true,
  billing: {
    first_name: 'John',
    last_name: 'Doe',
    address_1: '969 Market',
    address_2: '',
    city: 'San Francisco',
    state: 'CA',
    postcode: '94103',
    country: 'US',
    email: 'john.doe@example.com',
    phone: '(555) 555-5555'
  },
  shipping: {
    first_name: 'John',
    last_name: 'Doe',
    address_1: '969 Market',
    address_2: '',
    city: 'San Francisco',
    state: 'CA',
    postcode: '94103',
    country: 'US'
  },
  line_items: [
    {
      product_id: 93,
      quantity: 2
    },
    {
      product_id: 22,
      variation_id: 23,
      quantity: 1
    }
  ],
  shipping_lines: [
    {
      method_id: 'flat_rate',
      method_title: 'Flat Rate',
      total: 10
    }
  ]
};
llermaly
  • 2,331
  • 2
  • 16
  • 29

1 Answers1

0

This was my workaround :

  $http({
    method: 'POST',
    url: appConfig.DOMAIN_URL + '/wp-json/wc/v1/orders' ,
    //headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    headers: {'Content-Type': 'application/json'},
    //paramSerializer: '$httpParamSerializerJQLike',
    params: {
          consumer_key: appConfig.KEY,
          consumer_secret: appConfig.SECRET_KEY
        },
    data: {
      line_items: items,
      customer_id: 1,
      status: 'pending',
      shipping: {
          first_name: address.full_name,
          address_1: address.street,
          city: address.city,
          postcode: address.postal_code,
          state: address.state
        },
      shipping_lines: [
        {
          method_id: 'flat_rate',
          method_title: 'Flat Rate',
          total: tax
        }
      ]
    }
    })
    .then(function(res){
      dfd.resolve(res);
    }, function(error){
      dfd.reject(error);
    })
    return dfd.promise;
  }
})
;

Not sure if this is the cleanest way to pass the parameters but I could not understand why paramserializer was not working. I took some code from another sources.

Hope it helps

llermaly
  • 2,331
  • 2
  • 16
  • 29