0

Hi i have a an api file for fetching orders coming in my magento2 store , this is the code below which is successfully fetching my order number 66 from database , **`

i wants to know how can i fetch only last order placed in my list ?

`** so that i can get all upcoming latest orders.


<?php
$adminUrl='http://host.pk/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "user11", "password" => "pass11");                                                                    
$data_string = json_encode($data);                       
$ch = curl_init($adminUrl); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);       
$token = curl_exec($ch);
$token=  json_decode($token);   

//Use above token into header
$headers = array("Authorization: Bearer $token"); 

$requestUrl='http://host.pk/index.php/rest/V1/orders/66';


$ch = curl_init();
$ch = curl_init($requestUrl); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   

$result = curl_exec($ch);
$result=  json_decode($result);

 echo "<pre>";
 print_r($result);
echo "</pre>";
?>
1

Try this: I have added a DESC sort order for order created_at attribute. Which gives the last created order.

<?php
$adminUrl='http://host.pk/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "user11", "password" => "pass11");                                                                    
$data_string = json_encode($data);                       
$ch = curl_init($adminUrl); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);       
$token = curl_exec($ch);
$token=  json_decode($token);   

//Use above token into header
$headers = array("Authorization: Bearer $token"); 

$requestUrl='http://host.pk/index.php/rest/V1/orders?searchCriteria[pageSize]=1&searchCriteria[currentPage]=1&searchCriteria[sortOrders][0][field]=created_at&searchCriteria[sortOrders][0][direction]=DESC';


$ch = curl_init();
$ch = curl_init($requestUrl); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   

$result = curl_exec($ch);
$result=  json_decode($result);

 echo "<pre>";
 print_r($result);
echo "</pre>";
?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.