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.

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.

  • 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

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.