I want to get order,customer information by using REST API.
Anyone have idea about this?
-
You want to get order, customer info using REST API?Khoa Truong– Khoa Truong2017年01月11日 10:02:08 +00:00Commented Jan 11, 2017 at 10:02
-
Yes, I want order,customer info using REST API @KhoaTruongDinhBornCoder– BornCoder2017年01月11日 10:03:18 +00:00Commented Jan 11, 2017 at 10:03
5 Answers 5
For 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);
-
I am using same code but facing problem in getting password. For Password , $this->authSession->getUser()->getPassword(); but its returning encrypted password so rest api throw an error that you are not login properly. So, How to get decrypted password?BornCoder– BornCoder2017年01月11日 11:26:36 +00:00Commented Jan 11, 2017 at 11:26
-
Password encryption is one way. So you can't decrypt.Sohel Rana– Sohel Rana2017年01月11日 11:30:43 +00:00Commented Jan 11, 2017 at 11:30
-
But with encrypted password , I am not getting token here. So, How to get token with encrypted password ? Error: {"message":"You did not sign in correctly or your account is temporarily disabled."}BornCoder– BornCoder2017年01月11日 11:34:49 +00:00Commented Jan 11, 2017 at 11:34
-
How can I create my magento api to use this code snippet ?Ajwad Syed– Ajwad Syed2019年04月07日 15:20:24 +00:00Commented Apr 7, 2019 at 15:20
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
-
but how to implement REST api ?BornCoder– BornCoder2017年01月11日 10:21:13 +00:00Commented Jan 11, 2017 at 10:21
-
Read my answer here: magento.stackexchange.com/a/149927/33057Khoa Truong– Khoa Truong2017年01月11日 10:22:18 +00:00Commented Jan 11, 2017 at 10:22
-
could you please tell what are the parameters in body for the post man ?Abilash Erikson– Abilash Erikson2020年10月04日 07:55:22 +00:00Commented Oct 4, 2020 at 7:55
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
}
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.
-
2not work it return all result not only 5 recordsHaFiz Umer– HaFiz Umer2020年05月07日 16:30:59 +00:00Commented May 7, 2020 at 16:30
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/