I created the module for.

1. Decrease Stock quantity when invoice generate and
2. decrease of salable quantity and actual stock" after creating the ship.

But the module is not working properly actually salable quantity decrease twice after invoice generate and stock quantity also decrease twice after ship generate. Now to proper decrement of stock and salable I added code $qty=0; in line number 89 in file/vendor/magento/module-inventory-source-deduction-api/Model/SourceDeductionService.php. How can I override it in my below module obsever.php any minor help will be appreciated here my module.



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Meetanshi_Extension" setup_version="2.0.0">


        namespace Meetanshi\Extension\Observer;
        use Magento\Framework\Event\ObserverInterface;
        use Magento\Framework\Event\Observer;
        use Magento\CatalogInventory\Api\StockRegistryInterface;
        use Magento\Catalog\Model\ProductRepository;
        class UpdateInventory implements ObserverInterface
            protected $productRepository;
            protected $stockRegistry;
            public function __construct(
                ProductRepository $productRepository,
                StockRegistryInterface $stockRegistry
                $this->productRepository = $productRepository;
                $this->stockRegistry = $stockRegistry;
            public function execute(Observer $observer)
                try {
                    $invoice = $observer->getEvent()->getInvoice();
                    $invoiceItems = $invoice->getAllItems();
                    foreach ($invoiceItems as $item) {
                        $productId = $item->getProductId();
                        $product = $this->productRepository->getById($productId);
                        $sku = $product->getSku();
                        $stockItem = $this->stockRegistry->getStockItemBySku($sku);
                        $qty = $stockItem->getQty() - $item->getQty();
                        $this->stockRegistry->updateStockItemBySku($sku, $stockItem);
                } catch (\Exception $e) {
                    return $e->getMessage();


<?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_order_invoice_save_after">
            <observer name="Meetanshi_sales_order_invoice_save_after" instance="Meetanshi\Extension\Observer\UpdateInventory"/>
  • Do you want to override SourceDeductionService file right? Oct 5, 2019 at 8:25
  • yes .... need to put $qty=0 (this is for decrease stock qty 1) on line 88.
    – irshad
    Oct 5, 2019 at 8:46
  • But you didn't accept any answers even it is correct...To increase your reputation and for the one who is answering reputation and for others reference you must accept correct answers... Oct 5, 2019 at 8:49
  • You can accept the tick mark near the answer...(Left side under the votes) Oct 5, 2019 at 8:59
  • Thank you....Coming back to the question...declaring $qty=0 in line number 88 is correct? Oct 5, 2019 at 9:04


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.