1

尝试通过 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() );
    }
}   

我确实检查了这篇文章: https ://github.com/woothemes/woocommerce/issues/8561

而且我可以确认没有任何 .htacess 文件有这个问题。甚至看起来像。

4

1 回答 1

1

SO不接受仅链接的答案:)所以我将提供一些解释。您使用的数据结构无效,请在以下线程中查看我的回答:Woocommerce create order with hebrew text using the api retuns an error

下面给出正确的数据结构供您参考。它可能缺少一些键,有关您可以传递给 API 的键/值对的完整列表,请参阅此处的文档:http ://woothemes.github.io/woocommerce-rest-api-docs/#create-订单

$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                   
                )               
            ),


   ));
于 2015-09-22T15:32:13.573 回答