Can anyone let me know if we can Reorder in Magento2 using REST webapi? Actually i have made one api and used same code as magento default reorder does, here is my code in API function using order_id as parameter:
public function reOrder($orderId)
{
$order = $this->orderApiRepository->get($orderId);
$cart = $this->cart;
$items = $order->getItemsCollection();
foreach ($items as $item) {
try {
$cart->addOrderItem($item);
} catch (\Exception $e) {
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()), $e);
}
}
}
The issue is when i am checking response in postman, it returns null instead cart data, please let me know if anyone has done same type of things. please check below screenshot of POSTMAN api:
Best Answer
Use below code for Reorder api
Create YourNameSpace/Module/etc/webapi.xml
Create YourNameSpace/Module/Api/ReorderInterface.php
Create YourNameSpace\Module\Model\Reorder.php
Pass order id in your api with customer authentication , so it will add your order items in customer cart and gives you true in return so later on you can get cart items by calling /V1/carts/mine/items api.
Hope it will solve your issue.