0

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:

POSTMAN API

  • Got any solution ? – Yann Martel Jan 2 at 10:43
  • have you got solution for this – Adarsh Shukla Feb 19 at 11:51
  • Nope didn't get any solution on it – bhargav shastri Feb 19 at 13:06
  • Have you got any solution for this? – amesh Nov 13 at 8:53
0

Use below code for Reorder api

Create YourNameSpace/Module/etc/webapi.xml

<route url="/V1/reorder/:orderId" method="POST">
    <service class="YourNameSpace\Module\Api\ReorderInterface" method="createReorder"/>
    <resources>
        <resource ref="self" />
    </resources>
    <data>
        <parameter name="cartId" force="true">%cart_id%</parameter>
    </data>
</route>

Create YourNameSpace/Module/Api/ReorderInterface.php

    <?php

namespace YourNameSpace\Module\Api;

interface ReorderInterface
{
    /**
     * 
     *
     * @api
     * @param int $cartId
     * @param int $orderId
     * @return boolean
     */
    public function createReorder($cartId,$orderId);

}

Create YourNameSpace\Module\Model\Reorder.php

<?php

namespace YourNameSpace\Module\Sales\Model;

use YourNameSpace\Module\Api\ReorderInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Sales\Model\OrderRepository;
use Magento\Quote\Model\Quote;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Quote\Api\Data\CartItemInterface;
use Magento\Quote\Api\CartItemRepositoryInterface;

class Reorder implements ReorderInterface
{
    protected $quoteRepository;

    protected $orderRepository;

    protected $quote;

    protected $productRepository;

    protected $cartItemInterface;

    protected $cartItemRepository;

    public function __construct(
        CartRepositoryInterface $quoteRepository,
        OrderRepository $orderRepository,
        Quote $quote,
        ProductRepositoryInterface $productRepository,
        CartItemInterface $cartItemInterface,
        CartItemRepositoryInterface $cartItemRepository
    )
    {
        $this->quoteRepository = $quoteRepository;
        $this->orderRepository = $orderRepository;
        $this->quote = $quote;
        $this->productRepository = $productRepository;
        $this->cartItemInterface = $cartItemInterface;
        $this->cartItemRepository = $cartItemRepository;
    }


    /**
     * @param $cartId
     * @param $orderId
     * @return bool
     */
    public function createReorder($cartId,$orderId)
    {
       // $quoteRepo = $this->quoteRepository->getActive($cartId);
        $order = $this->orderRepository->get($orderId);

        $items = $order->getItemsCollection();
        foreach ($items as $item) {

            try {
              $this->addOrderItem($item, $cartId);
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                return false;

            } catch (\Exception $e) {
                return false;
            }
        }

        return true;
    }

    public function addOrderItem($orderItem,$quoteId)
    {
        /* @var $orderItem \Magento\Sales\Model\Order\Item */
        if ($orderItem->getParentItem() === null) {
            //$storeId = $this->_storeManager->getStore()->getId();
            try {
                /**
                 * We need to reload product in this place, because products
                 * with the same id may have different sets of order attributes.
                 */
                $product = $this->productRepository->getById($orderItem->getProductId());

            } catch (NoSuchEntityException $e) {
                return $this;
            }

            $this->cartItemInterface->setQuoteId($quoteId);
            $this->cartItemInterface->setSku($product->getSku());
            $this->cartItemInterface->setQty($orderItem->getQtyOrdered());

            $this->cartItemRepository->save($this->cartItemInterface);

        }
    }

}

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.

  • If the product have any option, then this will not working. It shows error like 'You need to choose options for your item.'. Any solution to this? – amesh Nov 13 at 6:51

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.