无法创建订单 Woocommerce 休息 Api php

Cannot create order Woocommerce rest Api php

我尝试了以下代码并尝试使用 android 中的 post 值来下订单,但每次我在 chrome 上测试它时都会出现 http 500 错误,并且 Android 给出 volly 服务器错误。不知道我在哪里搞砸了,会得到认真的帮助。谢谢你 问我是否需要查看 android 代码,但我认为这无关紧要,因为我在 $data.

中给出了硬编码值
<?php

require_once( 'lib/woocommerce-api.php' );

$pageNum=1;
$pageNum=$_GET['page_num'];

$options = array(
'debug'           => true,
'return_as_array' => false,
'validate_url'    => false,
'timeout'         => 30,
'ssl_verify'      => false,
);

try {

$client = new WC_API_Client( 'https://www.move2mart.com/', 'ck_0afa3a49305683160fe34189553a660053bd4e6239', 'cs_7dc38a7b52c3fdd34qw61e34090be636ae3364b196', $options );

//$data=array();
$data=[
'payment_method' => 'cod',
'payment_method_title' => 'Cash on Delivery',
'set_paid' => false,
'billing' => [
    'first_name' => 'John',
    'last_name' => 'Doe',
    'address_1' => '969 Market',
    'city' => 'Karachi',
    'email' => 'princeali@testing.com',
    'phone' => '03123121995'
], 'line_items' => [

    [
        'product_id' => 779,
        'quantity' => 1
    ]
]
];  

print_r($client->post('orders', $data));

if($_POST !=null) {
}

else{
  echo "Null POST Request";
}   
} 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() );
   }
}
$orderData = array(    
    "order" => array(
        "billing_address" => array(
            array(
                "first_name" => "",
                "last_name" => "",
                "company" => "",
                "address_1" => "",
                "address_2" => "",
                "city" => "",
                "state" => "",
                "postcode" => "",
                "country" => "",
                "email" => "",
                "phone" => "",
            )
        ),
        "shipping_address" => array(
            array(
                "first_name" => "",
                "last_name" => "",
                "company" => "",
                "address_1" => "",
                "address_2" => "",
                "city" => "",
                "state" => "",
                "postcode" => "",
                "country" => "",
            )
        ),
        "customer_id" => 1,
        "line_items" => array( 
            array(
                "product_id" => 1, 
                "quantity" => 1
            ) 
        )
    )
);

$client->orders->create($orderData);

你能试试上面的代码吗?

经过一番研究,我终于弄明白了。以下是 woocommerce 结帐网络服务的工作代码,可能会对您有所帮助 -

 /*** Just Copy & Paste and change your varriables **/

    //do your initial stuff 
    header('Content-type: application/json');
    $json_file=file_get_contents('php://input');
    $jsonvalue= json_decode($json_file,true);

     $user_id = $jsonvalue['user_id']; 
     $product_id = $jsonvalue['product_id']; 
     $quantity = $jsonvalue['quantity'];    

 //start order data 
 $orderData = array(    
     "order" => array(
     'payment_method' => 'paypal',
     'payment_method_title' => 'Paypal',
     'set_paid' => true,
    "billing_address" => array(
                                    "first_name" => "bfname",
                                    "last_name" => "blname",
                                    "company" => "testcompanybilling",
                                    "address_1" => "sec8",
                                    "address_2" => "e32",
                                    "city" => "noida",
                                    "state" => "Noida",
                                    "postcode" => "99999",
                                    "country" => "IN",
                                    "email" => "test@gmail.com",
                                    "phone" => "888899999999"

                            ),
      "shipping_address" => array(
                                    "first_name" => "sfname",
                                    "last_name" => "slname",
                                    "company" =>  "testcompanyshipping",
                                    "address_1" => "shakkarpur",
                                    "address_2" => "laxminigar",
                                    "city" => "New Delhi",
                                    "state" => "Delhi",
                                    "postcode" => "110092",
                                    "country" => "IN",
                                    "email" => "testsh@gmail.com",
                                    "phone" => "11009999"   
                              ),
    "customer_id" => $user_id,
    "line_items" => array( 
        array(
            "product_id" => $product_id, 
            "quantity" => $quantity
        ) 
      ),
    'shipping_lines' => array(
    array(
        'method_id' => 'flat_rate',
        'method_title' => 'Flat Rate',
        'total' => 10
    )
)
   )
);

//Create order usind order data
 $data =  $client->orders->create($orderData);
//echo '<pre>';
 //print_r($data);
$result['success']='true';
$result['error']="0";
$result['msg']='Your order has been successfully placed.';  
$result['data']=$data;  

 echo json_encode($result); `

干杯!!!