For some reason order canceled in bulk through my customly added order cancel mass action. Now I need to uncancel all orders.

  • Change status from cancel to the status that was previously set on order
  • Change state of order that was previously set
  • Subtract all quantities of products which are used in the order that are just reverted by cancelling.
  • Remove order history comments

Any help, experience and knowledge sharing would be highly appreciated.


Create a helper in any of your module


And use following code:


namespace Vendor\Module\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper;
use Magento\Sales\Model\Order;
use Magento\CatalogInventory\Model\Indexer\Stock\Processor as StockProcessor;
class Data extends AbstractHelper
     * @var \Magento\CatalogInventory\Api\StockManagementInterface
    protected $stockManagement;
     * @var StockProcessor
    protected $stockIndexerProcessor;

    public function __construct(
        Helper\Context $context,
        \Magento\CatalogInventory\Api\StockManagementInterface $stockManagement,
        StockProcessor $stockIndexerProcessor

    public function uncancleorder($order, $comment = "")
            throw new \Magento\Framework\Exception\LocalizedException(__('Invalid Order'));

        if ($order->isCanceled()) {
            $state = Order::STATE_PROCESSING;
            $productStockQty = [];
            foreach ($order->getAllVisibleItems() as $item) {
                $productStockQty[$item->getProductId()] = $item->getQtyCanceled();
                foreach ($item->getChildrenItems() as $child) {
                    $productStockQty[$child->getProductId()] = $item->getQtyCanceled();

            if (!empty($comment)) {
                $order->addStatusHistoryComment($comment, false);

            /* Reverting inventory */
            $itemsForReindex = $this->stockManagement->registerProductsSale(
            $productIds = [];
            foreach ($itemsForReindex as $item) {
                $productIds[] = $item->getProductId();
            if (!empty($productIds)) {


        } else {
            throw new \Magento\Framework\Exception\LocalizedException(__('We cannot un-cancel this order.'));
        return true;

Note: Edit this helper according to your requirements.

Now call uncancel method anywhere, just pass order object and comment(if required)

Reference module: https://github.com/Genmato/M2_UnCancelOrder

  • 1
    Useful and detailed answer. Apr 25 '19 at 11:44
  • How to support MSI in Magento 2.3?
    – Nausif
    Sep 17 '19 at 3:58
  • It misses coupon/store credit/promotions handling. Mar 9 at 14:22
  • 1
    Worked well for me. Thanks broski
    – rupi
    Jun 3 at 8:01

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.