错误:客户ID无效 - WooCommerce Rest Api - PHP

时间:2015-09-21 20:19:30

标签: php wordpress rest woocommerce woocommerce-rest-api

尝试通过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文件都没有这个问题。或者甚至看起来像。

1 个答案:

答案 0 :(得分:1)

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

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


   ));