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

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

尝试通过 WooCommerce Rest 创建订单时 Api,我收到此错误消息:错误:客户 ID 无效。

但是客户 ID 在请求中:

$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 = 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() );
    }
}   

我检查过这个post: https://github.com/woothemes/woocommerce/issues/8561

而且我可以确认 none 个 .htacess 文件有这个问题。甚至长得像。

Link 只有答案不被接受 :) 所以我会提供一些解释。您使用的数据结构无效,请在以下主题中查看我的回答:

下面给出了正确的数据结构,供大家参考。它可能缺少一些键,有关可以传递给 API 的 key/value 对的完整列表,请参阅此处的文档: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                   
                )               
            ),


   ));