Magento 2 - Reverting cancel orders programmaticallyPlacing orders programmatically from 3rd party ordering system“Cannot save shipment.” error with non-sample orders. 1062 Duplicate entry * for key 'UNQ_SALES_FLAT_SHIPMENT_INCREMENT_ID'error when i cancel an orderHow to get a collection of sales_order_status_history models with a specific set of id's for parent_id column?Does Magento2 cancel orders after 2 hours?Create a frontend controller with 'adminhtml' app state codeForce Order CompletionWhere is “status” located in the database from Magento 2 order gridOrder Status : Pending Payment vs. Payment ReviewMagento 2.3 - Fetching child/associated products of configurable product only returns In Stock products

Do I have to worry about players making “bad” choices on level up?

Why is current rating for multicore cable lower than single core with the same cross section?

Do I have an "anti-research" personality?

What does YCWCYODFTRFDTY mean?

When and why did journal article titles become descriptive, rather than creatively allusive?

Are Boeing 737-800’s grounded?

When did stoichiometry begin to be taught in U.S. high schools?

How to create an ad-hoc wireless network in Ubuntu

How to stop co-workers from teasing me because I know Russian?

Is it possible to Ready a spell to be cast just before the start of your next turn by having the trigger be an ally's attack?

Confusion about capacitors

What does "rf" mean in "rfkill"?

What are the spoon bit of a spoon and fork bit of a fork called?

Why do Ichisongas hate elephants and hippos?

Examples of non trivial equivalence relations , I mean equivalence relations without the expression " same ... as" in their definition?

Why “le” behind?

Transfer over $10k

Were there two appearances of Stan Lee?

Weird result in complex limit

How to set the font color of quantity objects (Version 11.3 vs version 12)?

Feels like I am getting dragged in office politics

Are some sounds more pleasing to the ear, like ㄴ and ㅁ?

How to creep the reader out with what seems like a normal person?

You look catfish vs You look like a catfish



Magento 2 - Reverting cancel orders programmatically


Placing orders programmatically from 3rd party ordering system“Cannot save shipment.” error with non-sample orders. 1062 Duplicate entry * for key 'UNQ_SALES_FLAT_SHIPMENT_INCREMENT_ID'error when i cancel an orderHow to get a collection of sales_order_status_history models with a specific set of id's for parent_id column?Does Magento2 cancel orders after 2 hours?Create a frontend controller with 'adminhtml' app state codeForce Order CompletionWhere is “status” located in the database from Magento 2 order gridOrder Status : Pending Payment vs. Payment ReviewMagento 2.3 - Fetching child/associated products of configurable product only returns In Stock products






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















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.










share|improve this question




























    2















    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.










    share|improve this question
























      2












      2








      2








      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.










      share|improve this question














      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.







      magento2 sales-order cancel-orders






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 25 at 7:05









      Muhammad HashamMuhammad Hasham

      3,33621236




      3,33621236




















          1 Answer
          1






          active

          oldest

          votes


















          0














          Create a helper in any of your module



          app/code/Vendor/Module/Helper/Data.php


          And use following code:



          <?php

          namespace VendorModuleHelper;
          use MagentoFrameworkAppHelperAbstractHelper;
          use MagentoFrameworkAppHelper;
          use MagentoSalesModelOrder;
          use MagentoCatalogInventoryModelIndexerStockProcessor as StockProcessor;
          class Data extends AbstractHelper

          /**
          * @var MagentoCatalogInventoryApiStockManagementInterface
          */
          protected $stockManagement;
          /**
          * @var StockProcessor
          */
          protected $stockIndexerProcessor;

          public function __construct(
          HelperContext $context,
          MagentoCatalogInventoryApiStockManagementInterface $stockManagement,
          StockProcessor $stockIndexerProcessor
          )

          parent::__construct($context);


          public function uncancleorder($order, $comment = "")

          if(!($order))
          throw new MagentoFrameworkExceptionLocalizedException(__('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();
          $child->setQtyCanceled(0);
          $child->setTaxCanceled(0);
          $child->setDiscountTaxCompensationCanceled(0);

          $item->setQtyCanceled(0);
          $item->setTaxCanceled(0);
          $item->setDiscountTaxCompensationCanceled(0);


          $order->setSubtotalCanceled(0);
          $order->setBaseSubtotalCanceled(0);
          $order->setTaxCanceled(0);
          $order->setBaseTaxCanceled(0);
          $order->setShippingCanceled(0);
          $order->setBaseShippingCanceled(0);
          $order->setDiscountCanceled(0);
          $order->setBaseDiscountCanceled(0);
          $order->setTotalCanceled(0);
          $order->setBaseTotalCanceled(0);
          $order->setState($state)
          ->setStatus($order->getConfig()->getStateDefaultStatus($state));
          if (!empty($comment))
          $order->addStatusHistoryComment($comment, false);


          /* Reverting inventory */
          $itemsForReindex = $this->stockManagement->registerProductsSale(
          $productStockQty,
          $order->getStore()->getWebsiteId()
          );
          $productIds = [];
          foreach ($itemsForReindex as $item)
          $item->save();
          $productIds[] = $item->getProductId();

          if (!empty($productIds))
          $this->stockIndexerProcessor->reindexList($productIds);

          $order->setInventoryProcessed(true);

          $order->save();

          else
          throw new MagentoFrameworkExceptionLocalizedException(__('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






          share|improve this answer


















          • 1





            Useful and detailed answer.

            – Muhammad Hasham
            Apr 25 at 11:44











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "479"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f272376%2fmagento-2-reverting-cancel-orders-programmatically%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Create a helper in any of your module



          app/code/Vendor/Module/Helper/Data.php


          And use following code:



          <?php

          namespace VendorModuleHelper;
          use MagentoFrameworkAppHelperAbstractHelper;
          use MagentoFrameworkAppHelper;
          use MagentoSalesModelOrder;
          use MagentoCatalogInventoryModelIndexerStockProcessor as StockProcessor;
          class Data extends AbstractHelper

          /**
          * @var MagentoCatalogInventoryApiStockManagementInterface
          */
          protected $stockManagement;
          /**
          * @var StockProcessor
          */
          protected $stockIndexerProcessor;

          public function __construct(
          HelperContext $context,
          MagentoCatalogInventoryApiStockManagementInterface $stockManagement,
          StockProcessor $stockIndexerProcessor
          )

          parent::__construct($context);


          public function uncancleorder($order, $comment = "")

          if(!($order))
          throw new MagentoFrameworkExceptionLocalizedException(__('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();
          $child->setQtyCanceled(0);
          $child->setTaxCanceled(0);
          $child->setDiscountTaxCompensationCanceled(0);

          $item->setQtyCanceled(0);
          $item->setTaxCanceled(0);
          $item->setDiscountTaxCompensationCanceled(0);


          $order->setSubtotalCanceled(0);
          $order->setBaseSubtotalCanceled(0);
          $order->setTaxCanceled(0);
          $order->setBaseTaxCanceled(0);
          $order->setShippingCanceled(0);
          $order->setBaseShippingCanceled(0);
          $order->setDiscountCanceled(0);
          $order->setBaseDiscountCanceled(0);
          $order->setTotalCanceled(0);
          $order->setBaseTotalCanceled(0);
          $order->setState($state)
          ->setStatus($order->getConfig()->getStateDefaultStatus($state));
          if (!empty($comment))
          $order->addStatusHistoryComment($comment, false);


          /* Reverting inventory */
          $itemsForReindex = $this->stockManagement->registerProductsSale(
          $productStockQty,
          $order->getStore()->getWebsiteId()
          );
          $productIds = [];
          foreach ($itemsForReindex as $item)
          $item->save();
          $productIds[] = $item->getProductId();

          if (!empty($productIds))
          $this->stockIndexerProcessor->reindexList($productIds);

          $order->setInventoryProcessed(true);

          $order->save();

          else
          throw new MagentoFrameworkExceptionLocalizedException(__('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






          share|improve this answer


















          • 1





            Useful and detailed answer.

            – Muhammad Hasham
            Apr 25 at 11:44















          0














          Create a helper in any of your module



          app/code/Vendor/Module/Helper/Data.php


          And use following code:



          <?php

          namespace VendorModuleHelper;
          use MagentoFrameworkAppHelperAbstractHelper;
          use MagentoFrameworkAppHelper;
          use MagentoSalesModelOrder;
          use MagentoCatalogInventoryModelIndexerStockProcessor as StockProcessor;
          class Data extends AbstractHelper

          /**
          * @var MagentoCatalogInventoryApiStockManagementInterface
          */
          protected $stockManagement;
          /**
          * @var StockProcessor
          */
          protected $stockIndexerProcessor;

          public function __construct(
          HelperContext $context,
          MagentoCatalogInventoryApiStockManagementInterface $stockManagement,
          StockProcessor $stockIndexerProcessor
          )

          parent::__construct($context);


          public function uncancleorder($order, $comment = "")

          if(!($order))
          throw new MagentoFrameworkExceptionLocalizedException(__('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();
          $child->setQtyCanceled(0);
          $child->setTaxCanceled(0);
          $child->setDiscountTaxCompensationCanceled(0);

          $item->setQtyCanceled(0);
          $item->setTaxCanceled(0);
          $item->setDiscountTaxCompensationCanceled(0);


          $order->setSubtotalCanceled(0);
          $order->setBaseSubtotalCanceled(0);
          $order->setTaxCanceled(0);
          $order->setBaseTaxCanceled(0);
          $order->setShippingCanceled(0);
          $order->setBaseShippingCanceled(0);
          $order->setDiscountCanceled(0);
          $order->setBaseDiscountCanceled(0);
          $order->setTotalCanceled(0);
          $order->setBaseTotalCanceled(0);
          $order->setState($state)
          ->setStatus($order->getConfig()->getStateDefaultStatus($state));
          if (!empty($comment))
          $order->addStatusHistoryComment($comment, false);


          /* Reverting inventory */
          $itemsForReindex = $this->stockManagement->registerProductsSale(
          $productStockQty,
          $order->getStore()->getWebsiteId()
          );
          $productIds = [];
          foreach ($itemsForReindex as $item)
          $item->save();
          $productIds[] = $item->getProductId();

          if (!empty($productIds))
          $this->stockIndexerProcessor->reindexList($productIds);

          $order->setInventoryProcessed(true);

          $order->save();

          else
          throw new MagentoFrameworkExceptionLocalizedException(__('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






          share|improve this answer


















          • 1





            Useful and detailed answer.

            – Muhammad Hasham
            Apr 25 at 11:44













          0












          0








          0







          Create a helper in any of your module



          app/code/Vendor/Module/Helper/Data.php


          And use following code:



          <?php

          namespace VendorModuleHelper;
          use MagentoFrameworkAppHelperAbstractHelper;
          use MagentoFrameworkAppHelper;
          use MagentoSalesModelOrder;
          use MagentoCatalogInventoryModelIndexerStockProcessor as StockProcessor;
          class Data extends AbstractHelper

          /**
          * @var MagentoCatalogInventoryApiStockManagementInterface
          */
          protected $stockManagement;
          /**
          * @var StockProcessor
          */
          protected $stockIndexerProcessor;

          public function __construct(
          HelperContext $context,
          MagentoCatalogInventoryApiStockManagementInterface $stockManagement,
          StockProcessor $stockIndexerProcessor
          )

          parent::__construct($context);


          public function uncancleorder($order, $comment = "")

          if(!($order))
          throw new MagentoFrameworkExceptionLocalizedException(__('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();
          $child->setQtyCanceled(0);
          $child->setTaxCanceled(0);
          $child->setDiscountTaxCompensationCanceled(0);

          $item->setQtyCanceled(0);
          $item->setTaxCanceled(0);
          $item->setDiscountTaxCompensationCanceled(0);


          $order->setSubtotalCanceled(0);
          $order->setBaseSubtotalCanceled(0);
          $order->setTaxCanceled(0);
          $order->setBaseTaxCanceled(0);
          $order->setShippingCanceled(0);
          $order->setBaseShippingCanceled(0);
          $order->setDiscountCanceled(0);
          $order->setBaseDiscountCanceled(0);
          $order->setTotalCanceled(0);
          $order->setBaseTotalCanceled(0);
          $order->setState($state)
          ->setStatus($order->getConfig()->getStateDefaultStatus($state));
          if (!empty($comment))
          $order->addStatusHistoryComment($comment, false);


          /* Reverting inventory */
          $itemsForReindex = $this->stockManagement->registerProductsSale(
          $productStockQty,
          $order->getStore()->getWebsiteId()
          );
          $productIds = [];
          foreach ($itemsForReindex as $item)
          $item->save();
          $productIds[] = $item->getProductId();

          if (!empty($productIds))
          $this->stockIndexerProcessor->reindexList($productIds);

          $order->setInventoryProcessed(true);

          $order->save();

          else
          throw new MagentoFrameworkExceptionLocalizedException(__('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






          share|improve this answer













          Create a helper in any of your module



          app/code/Vendor/Module/Helper/Data.php


          And use following code:



          <?php

          namespace VendorModuleHelper;
          use MagentoFrameworkAppHelperAbstractHelper;
          use MagentoFrameworkAppHelper;
          use MagentoSalesModelOrder;
          use MagentoCatalogInventoryModelIndexerStockProcessor as StockProcessor;
          class Data extends AbstractHelper

          /**
          * @var MagentoCatalogInventoryApiStockManagementInterface
          */
          protected $stockManagement;
          /**
          * @var StockProcessor
          */
          protected $stockIndexerProcessor;

          public function __construct(
          HelperContext $context,
          MagentoCatalogInventoryApiStockManagementInterface $stockManagement,
          StockProcessor $stockIndexerProcessor
          )

          parent::__construct($context);


          public function uncancleorder($order, $comment = "")

          if(!($order))
          throw new MagentoFrameworkExceptionLocalizedException(__('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();
          $child->setQtyCanceled(0);
          $child->setTaxCanceled(0);
          $child->setDiscountTaxCompensationCanceled(0);

          $item->setQtyCanceled(0);
          $item->setTaxCanceled(0);
          $item->setDiscountTaxCompensationCanceled(0);


          $order->setSubtotalCanceled(0);
          $order->setBaseSubtotalCanceled(0);
          $order->setTaxCanceled(0);
          $order->setBaseTaxCanceled(0);
          $order->setShippingCanceled(0);
          $order->setBaseShippingCanceled(0);
          $order->setDiscountCanceled(0);
          $order->setBaseDiscountCanceled(0);
          $order->setTotalCanceled(0);
          $order->setBaseTotalCanceled(0);
          $order->setState($state)
          ->setStatus($order->getConfig()->getStateDefaultStatus($state));
          if (!empty($comment))
          $order->addStatusHistoryComment($comment, false);


          /* Reverting inventory */
          $itemsForReindex = $this->stockManagement->registerProductsSale(
          $productStockQty,
          $order->getStore()->getWebsiteId()
          );
          $productIds = [];
          foreach ($itemsForReindex as $item)
          $item->save();
          $productIds[] = $item->getProductId();

          if (!empty($productIds))
          $this->stockIndexerProcessor->reindexList($productIds);

          $order->setInventoryProcessed(true);

          $order->save();

          else
          throw new MagentoFrameworkExceptionLocalizedException(__('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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 25 at 10:54









          Shoaib MunirShoaib Munir

          2,68421032




          2,68421032







          • 1





            Useful and detailed answer.

            – Muhammad Hasham
            Apr 25 at 11:44












          • 1





            Useful and detailed answer.

            – Muhammad Hasham
            Apr 25 at 11:44







          1




          1





          Useful and detailed answer.

          – Muhammad Hasham
          Apr 25 at 11:44





          Useful and detailed answer.

          – Muhammad Hasham
          Apr 25 at 11:44

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f272376%2fmagento-2-reverting-cancel-orders-programmatically%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Sum ergo cogito? 1 nng

          419 nièngy_Soadمي 19bal1.5o_g

          Queiggey Chernihivv 9NnOo i Zw X QqKk LpB