2

I wish to reorder programmatically I'm about to start it but not sure from where do I start.

I'm adding products to cart programmatically rest will done by Magento from Cart to Checkout.

So while adding product to cart how can I mark that it's reorder from particular order ID like 2.

Any suggestion? I've checked reoder controller

vendor/magento/module-sales/Controller/AbstractController/Reorder.php

But not getting idea to mark order as reorder.

  • Have you got any solution reorder programmatically? I am facing issue in programmatically reorder. Please share your solution. – Kirti Nariya Nov 29 '19 at 6:41
  • @KirtiNariya yes sure, I'll share in 24 hours. – Kaushal Suthar Nov 29 '19 at 8:58
  • Okay. Thanks. I will check. – Kirti Nariya Nov 29 '19 at 9:29
1

You can use Magento default reorder action. Before execute controller, add order to registry "current_order".
You can also copy code from execute method.

public function execute()
{
    orderId = 2;
    $order = $this->_orderFactory->load($orderId);
    $resultRedirect = $this->resultRedirectFactory->create();
    $cart = $this->_objectManager->get('Magento\Checkout\Model\Cart');
    $items = $order->getItemsCollection();
    foreach ($items as $item) {
        try {
            $cart->addOrderItem($item);
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            if ($this->_objectManager->get('Magento\Checkout\Model\Session')->getUseNotice(true)) {
                $this->messageManager->addNotice($e->getMessage());
            } else {
                $this->messageManager->addError($e->getMessage());
            }
            return $resultRedirect->setPath('*/*/history');
        } catch (\Exception $e) {
            $this->messageManager->addException($e, __('We can\'t add this item to your shopping cart right now.'));
            return $resultRedirect->setPath('checkout/cart');
        }
    }

    $cart->save();
}

Remember about DI and add correct factories.

| improve this answer | |
  • Okay I understand, Thanks for the code I'll try it out and will let you know. – Kaushal Suthar Mar 3 '17 at 12:27
  • class Magento\Checkout\Model\Cart already deprecated. But unfortunately there is no method like addOrderItem() :( Core controller \Magento\Sales\Controller\Order\Reorder is still using this deprecated class – Ruslan Mavlyanov Jan 31 at 15:14

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.