When trying to create an order via the WooCommerce Rest Api, I get this error message: Error: Customer ID is invalid.
However the Customer ID is in the request:
$data = '{
"order": {
"status": "processing",
"customer_id": 36,
"currency": "",
"billing_address": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": "",
"email": "",
"phone": ""
},
"shipping_address": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": ""
},
"line_items": [
{
"id": null,
"quantity": 1,
"product_id": 271,
},
{
"id": null,
"quantity": 3,
"product_id": 273,
}
],
}
}';
Full code
$client = new WC_API_Client( 'http://mywebsite.com', 'key', 'secret', $options );
$data = '{
"order": {
"status": "processing",
"customer_id": 36,
"currency": "",
"billing_address": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": "",
"email": "",
"phone": ""
},
"shipping_address": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": ""
},
"line_items": [
{
"id": null,
"quantity": 1,
"product_id": 271,
},
{
"id": null,
"quantity": 3,
"product_id": 273,
}
],
}
}';
$client->orders->create($data);
} catch ( WC_API_Client_Exception $e ) {
echo $e->getMessage() . PHP_EOL;
//echo $e->getCode() . PHP_EOL;
if ( $e instanceof WC_API_Client_HTTP_Exception ) {
print_r( $e->get_request() );
//print_r( $e->get_response() );
}
}
I did check this post:
https://github.com/woothemes/woocommerce/issues/8561
And I can confirm that none of the .htacess files have this problem. Or even look like it.
Link only answers are not accepted on SO 🙂 so I’ll provide some explanation. You are using an invalid data structure, please see my answer on the following thread : Woocommerce create order with hebrew text using the api retuns an error
Correct data structure is given below for your reference. It may be missing some keys, for a full list of key/value pairs that you can pass to the API, see the documentation here : http://woothemes.github.io/woocommerce-rest-api-docs/#create-an-order