How to Programmatically Reorder in Magento 2? (Get Code) 

Reordering allows customers to place a new order based on their previous order with the same items, details, and payment/shipping information. 

This helps shoppers to avoid going through the entire order placement process from scratch.

In this blog, learn to programmatically reorder in Magento 2, especially if you offer a subscription-based pricing model. 

Steps to Programmatically Reorder in Magento 2

Firstly, to get reorder details, you will need to load the information of the previous order. Then, create a new quote and quickly get the previous order value. 

Create a custom script or helper class at Vendor/Module/Helper and use the below code:

<?php

namespace Vendor\Module\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Sales\Model\OrderFactory;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\QuoteManagement;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Customer\Model\CustomerFactory;
use Magento\Store\Model\StoreManagerInterface;

class Reorder extends AbstractHelper
{
    protected $orderFactory;
    protected $quoteFactory;
    protected $quoteManagement;
    protected $cartRepository;
    protected $customerFactory;
    protected $storeManager;

    public function __construct(
        OrderFactory $orderFactory,
        QuoteFactory $quoteFactory,
        QuoteManagement $quoteManagement,
        CartRepositoryInterface $cartRepository,
        CustomerFactory $customerFactory,
        StoreManagerInterface $storeManager
    ) {
        $this->orderFactory = $orderFactory;
        $this->quoteFactory = $quoteFactory;
        $this->quoteManagement = $quoteManagement;
        $this->cartRepository = $cartRepository;
        $this->customerFactory = $customerFactory;
        $this->storeManager = $storeManager;
    }

    public function reorder($orderId)
    {
        // Load the original order
        $order = $this->orderFactory->create()->load($orderId);
        if (!$order->getId()) {
            throw new \Magento\Framework\Exception\LocalizedException(__('Order not found.'));
        }

        // Create a new quote
        $quote = $this->quoteFactory->create();
        $quote->setStoreId($this->storeManager->getStore()->getId());

        // Assign customer to the quote
        $customer = $this->customerFactory->create()->load($order->getCustomerId());
        $quote->assignCustomer($customer);

        // Add order items to the new quote
        foreach ($order->getAllVisibleItems() as $item) {
            $quote->addProduct($item->getProduct(), (int) $item->getQtyOrdered());
        }

        // Set billing and shipping addresses
        $quote->getBillingAddress()->addData($order->getBillingAddress()->getData());
        $quote->getShippingAddress()->addData($order->getShippingAddress()->getData());

        // Set shipping and payment methods
        $quote->getShippingAddress()->setCollectShippingRates(true)->collectShippingRates()
            ->setShippingMethod($order->getShippingMethod());
        $quote->setPaymentMethod($order->getPayment()->getMethod());
        $quote->setInventoryProcessed(false);

        // Collect totals and save quote
        $quote->collectTotals();
        $this->cartRepository->save($quote);

        // Convert quote to order
        $order = $this->quoteManagement->submit($quote);
        return $order;
    }
}

Then, call the reorder function with the order ID in the frontend controller as per your use case:

$order = $this->reorderHelper->reorder(100);

That’s it. You have created an order programmatically in Magento 2.  Try this solution and allow your shoppers to create a new order instantly. 

Table of Contents