I am using the exact example provided here:
http://woothemes.github.io/woocommerce-rest-api-docs/?shell#create-an-order
Which failes horribly:
Error: call to URL https://foo.com/wc-api/v3/orders?consumer_key=ck_bc4fd38b21ae4f5c44da3cb8a64ed5a0951e0902&consumer_secret=cs_d6cfb1faf914534955ba1b5e6063577e90610bc3 failed with status 400, response {"errors":[{"code":"woocommerce_api_missing_order_data","message":"Ingen order data angivet for at oprette order"}]}, curl_error , curl_errno 0
The PHP code used is this:
$url = 'https://foo.com/wc-api/v3/orders?consumer_key=ck_bc4fd38b21ae4f5c44da3cb8a64ed5a0951e0902&consumer_secret=cs_d6cfb1faf914534955ba1b5e6063577e90610bc3';
$json_string =
'{
"order": {
"payment_details": {
"method_id": "bacs",
"method_title": "Direct Bank Transfer",
"paid": true
},
"billing_address": {
"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_address": {
"first_name": "John",
"last_name": "Doe",
"address_1": "969 Market",
"address_2": "",
"city": "San Francisco",
"state": "CA",
"postcode": "94103",
"country": "US"
},
"customer_id": 2,
"line_items": [
{
"product_id": 1389,
"quantity": 1
}
],
"shipping_lines": [
{
"method_id": "flat_rate",
"method_title": "Flat Rate",
"total": 10
}
]
}
}';
$content = json_encode($json_string);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
It is so poorly documented how to do this that it is extremely annoying!