I want to get order,customer information by using REST API.
Anyone have idea about this?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityFor order information:
$userData = array("username" => "admin", "password" => "admin123"); $ch = curl_init("http://magento213/index.php/rest/V1/integration/admin/token"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Length: " . strlen(json_encode($userData)))); $token = curl_exec($ch); $ch = curl_init("http://magento213/index.php/rest/V1/orders/1"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token))); $result = curl_exec($ch); $result = json_decode($result, 1); echo '<pre>';print_r($result);
fetch all pending orders through REST API, add comments on it and change status for those orders
$userData = array("username" => "adminuser", "password" => "adminpassowrd");
$ch = curl_init("https://yourhost.com/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));
$token = curl_exec($ch);
$apiOrderUrl = 'https://yourhost.com/index.php/rest/V1/orders/?searchCriteria[filterGroups][0][filters][0][field]=status&searchCriteria[filterGroups][0][filters][0][value]=pending&searchCriteria[filterGroups][0][filters][0][conditionType]=eq';
$ch = curl_init($apiOrderUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
$results = json_decode($result, true); // all orders with status pending
foreach ($results['items'] as $order) {
$commentData = array(
'id' => $order['entity_id'], //order_id
'statusHistory' => array(
'comment' => 'Received order?',
'entity_id' => null,
'is_customer_notified' => '1',
'created_at' => now(),
'parent_id' => $order['entity_id'], //order_id
'entity_name' => 'order',
'status' => 'processing', //assign new status to order
'is_visible_on_front' => '1'
)
);
$commentData = json_encode($commentData,true);
$orderStatusApiUrl = 'https://yourhost.com/index.php/rest/V1/orders/'.$order['entity_id'].'/comments';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$orderStatusApiUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $commentData );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$response = curl_exec($ch); //true
}
Go to http://devdocs.magento.com/swagger/
salesOrderRepositoryV1 and customerCustomerRepositoryV1
Order info: GET: '/V1/orders/{id}'
Customer info: GET: '/V1/customers/{customerId}'
We can read more here: https://magento.stackexchange.com/a/149927/33057
For Get Order Collection using REST API, You can pass below request url to fetch order data from Magento.
{SITE_URL}/rest/V1/orders?searchCriteria=entity_id&limit=5
Now based on above criteria you can get 5 order from order collection.
For any reason if you need to fetch order collection, a search criteria has to be amended to the URL. If you need to fetch all the orders, please use empty search criteria.
Example : http://magento213/index.php/rest/V1/orders?searchCriteria
Swagger contains more details. Please explore http://devdocs.magento.com/swagger/