0

I tried the following code and tried to post values from android to place an order but every time i test it on chrome it give http 500 error, and Android gives volly server error. Dont know where i am messing up, serious help would be appriciated. Thank You Ask me if you need to take a look at the android Code, but i think that doesnot matter here because i am giving hard coded values in $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() );
   }
}
ArK
  • 20,698
  • 67
  • 109
  • 136
Prince Ali
  • 63
  • 2
  • 11
  • 1
    http://stackoverflow.com/questions/36729701/programmatically-creating-new-order-in-woocommerce/36929630#36929630 – Maha Dev Apr 25 '17 at 06:10
  • @MahaDev i am using Woocommerce REST API, am not coding directly in functions.php or the website itself. – Prince Ali Apr 25 '17 at 06:14
  • @MahaDev i followed this https://woocommerce.github.io/woocommerce-rest-api-docs/?php#order-properties – Prince Ali Apr 25 '17 at 06:16
  • @MahaDev tried still getting null at "billing_address":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":"","email":"","phone":""} – Prince Ali Apr 25 '17 at 07:47

2 Answers2

3
$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);

Would you please try above code?

Purvik Dhorajiya
  • 4,662
  • 3
  • 34
  • 43
  • ok just found an other issue, fileds are not properly getting mapped like if i put 'first_name' => 'John' it wont map it to orders, but it does create and order with a blank name. – Prince Ali Apr 25 '17 at 06:50
  • Sir you think you can also help me with that? – Prince Ali Apr 25 '17 at 06:57
  • tried still getting null at "billing_address":{"first_name":"","last_name":"","company":‌​"","address_1":"","a‌​ddress_2":"","city":‌​"","state":"","postc‌​ode":"","country":""‌​,"email":"","phone":‌​""} – Prince Ali Apr 25 '17 at 08:01
  • The only thing which is fine is line_items, products are the only thing showing up in order. – Prince Ali Apr 25 '17 at 08:06
  • yes, php won't compile with out the comma separator, i retried it with your latest edit, still no luck. – Prince Ali Apr 25 '17 at 08:47
  • @PrinceAli have you pass value on above mention field? ex: `first_name, lastname` etc... – Purvik Dhorajiya Apr 25 '17 at 09:10
  • Yes I did. I tested it through hardcoding values as well as getting from $_POST['name'] none worked – Prince Ali Apr 25 '17 at 09:14
  • Snaps are here: (1) http://prntscr.com/f0k3ae (2) http://prntscr.com/f0k3h7 – Prince Ali Apr 25 '17 at 09:17
  • @PrinceAli I apologise my answer, `billing_address` and `shipping_address` write in `order` array. I have updated my answer. Would you please check it? – Purvik Dhorajiya Apr 25 '17 at 09:28
  • Still getting null. – Prince Ali Apr 25 '17 at 13:07
  • **Snaps** (1) http://prntscr.com/f0mrn0 (2) http://prntscr.com/f0msfr – Prince Ali Apr 25 '17 at 13:11
  • ok i fixed is structured it according to the json null responce. Thank You so much for your cooperation. (Thumbs Up) – Prince Ali Apr 25 '17 at 13:50
  • @PurvikDhorajiya I am new here. I don't know more about how to place an order by using the REST API inside a native mobile application. I have tried this `/wp-json/wc/v3/orders?consumer_key=CONSUMER_KEY&consumer_secret=SECRET_KEY` Rest API for orders. But not understand how to get user cart product information, billing, and shipping information inside that POST API. So can you please help me here and also inform me where I need to do add your above-mentioned code inside my WooCommerce based website. – Ketan Nov 25 '19 at 07:27
1

Finally, I figured it out after some research. Below is working code of woocommerce checkout webservice that may Help you -

 /*** 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); `

cheers!!!