2

how to stop decreasing stock qty while placing an order in Magento 2 via REST API

Which plugin or which observer we have to use to stop decreasing stock qty?

I am using event "checkout_submit_all_after"

events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
     <event name="checkout_submit_all_after">
        <observer name="stop_decrease_qty" instance="CompanyName\ModuleName\Observer\StopDecreaseQty"/>
    </event>
</config>

StopDecreaseQty.php <?php namespace CompanyName\ModuleName\Observer;

class StopDecreaseQty implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       /** @var \Magento\Quote\Model\Quote $quote */
       $quote = $observer->getEvent()->getQuote();
       $quote->setInventoryProcessed(true); //not effect inventory
       return $this;
    }
}

But it is not working

1

I have an idea for you: leave magento do what it does best first with all its complexity and coupled features.. Then once the order is successfully placed, reincrement the stock for each item in the order.

Below I put an event when the order is successfull, and an observer with a code that shows you how the reincrement stock code could work.. There is still some lines of codes to add to have it fully working but hopefully it may help you going on the right track

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_model_service_quote_submit_success">
        <observer name="mbs_stock_order_save_after" instance="Mbs\StockTrick\Observer\OrderSaveAfter" />
    </event>


public function execute(Observer $observer)
{
    if ($this->quoteHasItems($observer)) {
        foreach ($this->getVisibleQuoteItems() as $item) {
            try {
                $this->productStockHandler->incrementStock($item);
            } catch (InvalidQuoteItem $e) {
                // possible log
            } 
        }
    }
}

I have written a full module for you. It works on my local, hopefully it resolves your issue. Keep me posted, thanks

https://bitbucket.org/magstaging/incrementstock/src

| improve this answer | |
  • this variable $this->productStockHandler i need to declare in construct ? – sumeet bajaj Jul 21 at 11:22
  • i am getting when use your code - Uncaught Magento\Framework\Exception\InputException: An ID is needed. Set the ID and try again. in /var/www/html/sessions.ca/vendor/magento/module-sales/Model/OrderRepository.php:133 – sumeet bajaj Jul 21 at 12:42
  • do put your code and we should be able to work out what is wrong – Herve Tribouilloy Jul 21 at 12:49
  • what i have to put code ? – sumeet bajaj Jul 21 at 13:31
  • just the observer and the events.xml file should be sufficient – Herve Tribouilloy Jul 21 at 13:37

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.