3
votes

I am using REST API V3 and creating an order via following and posting variation. Although I am sending variation_id correctly in line items of order object. Here is the request object I am sending -

URL: http://mywebsite.com/wc-api/v3/orders?.............
Request Method: POST
Status Code: 201 Created

ORDER OBJECT-

 {"order": {
"payment_method": "cod",
"payment_method_title": "Cash On Delivery",
"set_paid": false,
"billing_address": {
  "first_name": "I ey",
  "last_name": "Xhh",
  "address_1": "Gdb",
  "address_2": "Dvhs",
  "city": "Gsg",
  "state": "New Delhi",
  "postcode": "54619",
  "country": "India",
  "email": "[email protected]",
  "phone": "54646"
},
"shipping_address": {
  "first_name": "I ey",
  "last_name": "Xhh",
  "address_1": "Gdb",
  "address_2": "Dvhs",
  "city": "Gsg",
  "state": "New Delhi",
  "postcode": "54619",
  "country": "India",
  "email": "[email protected]",
  "phone": "54646"
},
"line_items": [
  {
    "product_id": 420,
    **"variation_id": 429,**
    "quantity": 1
  },
  {
    "product_id": 420,
    **"variation_id": 425,**
    "quantity": 1
  }
]}}

Unfortunately, RESPONSE returned after 201 does not have variation_id in line_items object -

"line_items": [
  {
    "id": 101,
    "subtotal": "5100.00",
    "subtotal_tax": "0.00",
    "total": "5100.00",
    "total_tax": "0.00",
    "price": "5100.00",
    "quantity": 1,
    "tax_class": "",
    "name": "Alpha Cement",
    "product_id": 95,
    "sku": "",
    "meta":[]
  },
  {
    "id": 102,
    "subtotal": "20000000.00",
    "subtotal_tax": "0.00",
    "total": "20000000.00",
    "total_tax": "0.00",
    "price": "20000000.00",
    "quantity": 1,
    "tax_class": "",
    "name": "Apple MacBook Core i5 7th Gen",
    "product_id": 94,
    "sku": "",
    "meta": [

    ]
  },
  {
    "id": 103,
    "subtotal": "82000.00",
    "subtotal_tax": "0.00",
    "total": "82000.00",
    "total_tax": "0.00",
    "price": "82000.00",
    "quantity": 1,
    "tax_class": "",
    "name": "CR SHEET",
    "product_id": 420,//correct product id
    "sku": "",
    "meta": [

    ]
  },
  {
    "id": 104,
    "subtotal": "82000.00",
    "subtotal_tax": "0.00",
    "total": "82000.00",
    "total_tax": "0.00",
    "price": "82000.00",
    "quantity": 1,
    "tax_class": "",
    "name": "CR SHEET",
    "product_id": 420, //correct product id
    "sku": "",
    "meta": [

    ]
  }
],

NOTE: An order is successfully received on WooCommerce side but that's for the main Product and not the variation.

Please help, if anyone seeing I am doing anything wrong here. Thanks!

2
Anyone please respond to my question.Kanak Sony
I would double-check that those variations are enabled and purchasable. The docs say a variation_id should be returned: woocommerce.github.io/woocommerce-rest-api-docs/…helgatheviking

2 Answers

0
votes

Are you sure you're formatting your request properly?

I looked at the official docs here and was able to get something similar to your request to work, assuming i removed the "orders" key and it comes back like so,

Request:

{
  "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": "[email protected]",
    "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": 843,
      "variation_id": 1540,
      "quantity": 1
    }
  ],
  "shipping_lines": [
    {
      "method_id": "flat_rate",
      "method_title": "Flat Rate",
      "total": "10"
    }
  ]
}

Response:

{
    "id": 1555,
    "parent_id": 0,
    "number": "1555",
    "order_key": "wc_order_5c33a7a124cc0",
    "created_via": "rest-api",
    "version": "3.5.1",
    "status": "processing",
    "currency": "USD",
    "date_created": "2019-01-07T14:25:22",
    "date_created_gmt": "2019-01-07T19:25:22",
    "date_modified": "2019-01-07T14:25:22",
    "date_modified_gmt": "2019-01-07T19:25:22",
    "discount_total": "0.00",
    "discount_tax": "0.00",
    "shipping_total": "10.00",
    "shipping_tax": "0.00",
    "cart_tax": "0.00",
    "total": "24.97",
    "total_tax": "0.00",
    "prices_include_tax": false,
    "customer_id": 0,
    "customer_ip_address": "",
    "customer_user_agent": "",
    "customer_note": "",
    "billing": {
        "first_name": "John",
        "last_name": "Doe",
        "company": "",
        "address_1": "969 Market",
        "address_2": "",
        "city": "San Francisco",
        "state": "CA",
        "postcode": "94103",
        "country": "US",
        "email": "[email protected]",
        "phone": "(555) 555-5555"
    },
    "shipping": {
        "first_name": "John",
        "last_name": "Doe",
        "company": "",
        "address_1": "969 Market",
        "address_2": "",
        "city": "San Francisco",
        "state": "CA",
        "postcode": "94103",
        "country": "US"
    },
    "payment_method": "bacs",
    "payment_method_title": "Direct Bank Transfer",
    "transaction_id": "",
    "date_paid": "2019-01-07T14:25:22",
    "date_paid_gmt": "2019-01-07T19:25:22",
    "date_completed": null,
    "date_completed_gmt": null,
    "cart_hash": "",
    "meta_data": [],
    "line_items": [
        {
            "id": 588,
            "name": "Chocolate Chip Cookie",
            "product_id": 93,
            "variation_id": 0,
            "quantity": 2,
            "tax_class": "",
            "subtotal": "5.98",
            "subtotal_tax": "0.00",
            "total": "5.98",
            "total_tax": "0.00",
            "taxes": [],
            "meta_data": [],
            "sku": "",
            "price": 2.99
        },
        {
            "id": 589,
            "name": "Salad (Copy) - Pint (Serves 2-3)",
            "product_id": 1537,
            "variation_id": 1540,
            "quantity": 1,
            "tax_class": "",
            "subtotal": "8.99",
            "subtotal_tax": "0.00",
            "total": "8.99",
            "total_tax": "0.00",
            "taxes": [],
            "meta_data": [
                {
                    "id": 5622,
                    "key": "size",
                    "value": "Pint (Serves 2-3)"
                }
            ],
            "sku": "",
            "price": 8.99
        }
    ],
    "tax_lines": [],
    "shipping_lines": [
        {
            "id": 590,
            "method_title": "Flat Rate",
            "method_id": "flat_rate",
            "instance_id": "",
            "total": "10.00",
            "total_tax": "0.00",
            "taxes": [],
            "meta_data": []
        }
    ],
    "fee_lines": [],
    "coupon_lines": [],
    "refunds": [],
    "_links": {
        "self": [
            {
                "href": "https://acme.local/wp-json/wc/v3/orders/1555"
            }
        ],
        "collection": [
            {
                "href": "https://acme.local/wp-json/wc/v3/orders"
            }
        ]
    }
}

If the above doesn't work for you (that is, reformatting your order data to match the above), you can always debug the issue on the woocommerce side of things by setting up XDebug and setting a breakpoint here:

wp-content/plugins/woocommerce/includes/api/class-wc-rest-orders-controller.php, specifically the "prepare_object_for_database" method, would be the first place i'd look, see if the data coming in to the $request variable is what you expect, and if not check your error log, ensure your content type is set to json, and that your json is in the request body, not the POST data.

0
votes

I use this. And this works for me:

{"payment_method": "cod",   "payment_method_title": "Cash on delivery",   "set_paid": false,   "billing": {     "first_name": "rocky",     "last_name": "",     "address_1": "bangladesh ",     "address_2": "",     "city": "dhaka",     "state": "Chittagong ",     "postcode": "4590",     "country": "",     "email": "[email protected]",     "phone": "01683946462"   },   "shipping": {     "first_name": "md roky",     "last_name": "",     "address_1": "bangladesh ",     "address_2": "",     "city": "rangamati ",     "state": "Chittagong ",     "postcode": "4590",     "country": "US"   },   "line_items": [     {       "product_id": 33, "variation_id":324,      "quantity": 2     }],   "shipping_lines": [     {       "method_id": "flat_rate",       "method_title": "Flat Rate",       "total": "80"     }   ] }