{"id":11494,"date":"2025-04-09T11:03:15","date_gmt":"2025-04-09T05:33:15","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/?p=11494"},"modified":"2025-04-10T12:56:46","modified_gmt":"2025-04-10T07:26:46","slug":"programmatically-reorder-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/programmatically-reorder-in-magento-2\/","title":{"rendered":"How to Programmatically Reorder in Magento 2? (Get Code)\u00a0"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Reordering allows customers to place a new order based on their previous order with the same items, details, and payment\/shipping information.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This helps shoppers to avoid going through the entire order placement process from scratch.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this blog, learn to programmatically reorder in Magento 2, especially if you offer a subscription-based pricing model.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Programmatically Reorder in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a custom script or<strong> helper class<\/strong> at <strong>Vendor\/Module\/Helper<\/strong> and use the below code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Helper;\n\nuse Magento\\Framework\\App\\Helper\\AbstractHelper;\nuse Magento\\Sales\\Model\\OrderFactory;\nuse Magento\\Quote\\Model\\QuoteFactory;\nuse Magento\\Quote\\Model\\QuoteManagement;\nuse Magento\\Quote\\Api\\CartRepositoryInterface;\nuse Magento\\Customer\\Model\\CustomerFactory;\nuse Magento\\Store\\Model\\StoreManagerInterface;\n\nclass Reorder extends AbstractHelper\n{\n    protected $orderFactory;\n    protected $quoteFactory;\n    protected $quoteManagement;\n    protected $cartRepository;\n    protected $customerFactory;\n    protected $storeManager;\n\n    public function __construct(\n        OrderFactory $orderFactory,\n        QuoteFactory $quoteFactory,\n        QuoteManagement $quoteManagement,\n        CartRepositoryInterface $cartRepository,\n        CustomerFactory $customerFactory,\n        StoreManagerInterface $storeManager\n    ) {\n        $this->orderFactory = $orderFactory;\n        $this->quoteFactory = $quoteFactory;\n        $this->quoteManagement = $quoteManagement;\n        $this->cartRepository = $cartRepository;\n        $this->customerFactory = $customerFactory;\n        $this->storeManager = $storeManager;\n    }\n\n    public function reorder($orderId)\n    {\n        \/\/ Load the original order\n        $order = $this->orderFactory->create()->load($orderId);\n        if (!$order->getId()) {\n            throw new \\Magento\\Framework\\Exception\\LocalizedException(__('Order not found.'));\n        }\n\n        \/\/ Create a new quote\n        $quote = $this->quoteFactory->create();\n        $quote->setStoreId($this->storeManager->getStore()->getId());\n\n        \/\/ Assign customer to the quote\n        $customer = $this->customerFactory->create()->load($order->getCustomerId());\n        $quote->assignCustomer($customer);\n\n        \/\/ Add order items to the new quote\n        foreach ($order->getAllVisibleItems() as $item) {\n            $quote->addProduct($item->getProduct(), (int) $item->getQtyOrdered());\n        }\n\n        \/\/ Set billing and shipping addresses\n        $quote->getBillingAddress()->addData($order->getBillingAddress()->getData());\n        $quote->getShippingAddress()->addData($order->getShippingAddress()->getData());\n\n        \/\/ Set shipping and payment methods\n        $quote->getShippingAddress()->setCollectShippingRates(true)->collectShippingRates()\n            ->setShippingMethod($order->getShippingMethod());\n        $quote->setPaymentMethod($order->getPayment()->getMethod());\n        $quote->setInventoryProcessed(false);\n\n        \/\/ Collect totals and save quote\n        $quote->collectTotals();\n        $this->cartRepository->save($quote);\n\n        \/\/ Convert quote to order\n        $order = $this->quoteManagement->submit($quote);\n        return $order;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then, call the reorder function with the order ID in the frontend controller as per your use case:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$order = $this->reorderHelper->reorder(100);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s it. You have created an order programmatically in Magento 2.\u00a0 Try this solution and allow your shoppers to create a new order instantly.\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reordering allows customers to place a new order based on their previous order with the same items, details, and payment\/shipping information.&nbsp; This helps shoppers to&#8230;<\/p>\n","protected":false},"author":5,"featured_media":11498,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-11494","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/11494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=11494"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/11494\/revisions"}],"predecessor-version":[{"id":11773,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/11494\/revisions\/11773"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media\/11498"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=11494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=11494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=11494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}