Back to Top

Magento 2: How To Restore Canceled Orders

Updated 29 September 2022

Hello Friends!!!

In this article, we will learn how we can restore canceled orders in Magento 2.

Sometimes we face problems restoring canceled orders and recovering purchase requests from the order view page. So In this article, we will see how to solve this kind of problem.

Restore Cancel Order Using Controller

<?php

namespace Vendor\ModuleName\Controller\ControllerName;

use Magento\Framework\App\Action\Action;

class RestoreCancelOrder extends Action
{
    
    public function __construct(
        Context $context,
        \Magento\Sales\Model\OrderFactory $orderFactory,
        \Magento\Sales\Model\Order $orderModel
    ) {      
        $this->orderFactory = $orderFactory;
        $this->orderModel = $orderModel;
        parent::__construct($context);
    }


    public function execute()
    {
        $orderId =121;
        $orderData = $this->orderFactory->create()->load($orderId);   
       if($orderData->isCanceled()) {  
        $order = $this->orderModel->load($orderId);
        $order->setState("processing")
            ->setStatus("processing")
            ->save();
                $orderItems = $order->getAllItems();
                foreach ($orderItems as $item) {
                    $item->setData("qty_canceled",0)->save();
            }
       }
      
        }

  }

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home