Taurian Taurian - 5 months ago 50
PHP Question

Error: Customer ID is invalid - WooCommerce Rest Api - PHP

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.

Answer

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

$p = $client->orders->create( 
        array (
            'payment_details' => array( 
                "method_id" => "bacs",
                "method_title" => "Direct Bank Transfer",
                "paid" => true
            ),

            'billing_address' => array( 
                "first_name" => "אֱלֹהִ֑ים",
                "last_name" => "Almighty",
                "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_address' => array( 
                "first_name" => "אֱלֹהִ֑ים",
                "last_name" => "Almighty",
                "address_1" => "969 Market",
                "address_2" => "",
                "city" => "San Francisco",
                "state" => "CA",
                "postcode" => "94103",
                "country" => "US"
            ),

            'line_items' => array(
                array( 
                    'product_id' => 579,
                    'quantity' => 2                     
                )               
            ),

            'shippling_lines' => array(
                array(
                    'method_id' => 'flat_rate',
                    'method_title' => 'Flat Rate',
                    'total' => 10                   
                )               
            ),


   ));