1

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.
Meetanshi/Extension/registration.php

<?php
 \Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Meetanshi_Extension',
    __DIR__
);

Meetanshi/Extension/etc/module.xml

<?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">
     </module>
</config>

Meetanshi/Extension/UpdateInventory.php

<?php
        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();
                        $stockItem->setQty($qty);
                        $stockItem->setIsInStock((bool)$qty);
                        $this->stockRegistry->updateStockItemBySku($sku, $stockItem);
                    }
                } catch (\Exception $e) {
                    return $e->getMessage();
                }
            }
        }

Meetanshi/Extension/etc/adminhtml/events.xml

<?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"/>
        </event>
</config>
  • Do you want to override SourceDeductionService file right? – Ranganathan Oct 5 '19 at 8:25
  • yes .... need to put $qty=0 (this is for decrease stock qty 1) on line 88. – irshad Oct 5 '19 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... – Ranganathan Oct 5 '19 at 8:49
  • You can accept the tick mark near the answer...(Left side under the votes) – Ranganathan Oct 5 '19 at 8:59
  • Thank you....Coming back to the question...declaring $qty=0 in line number 88 is correct? – Ranganathan Oct 5 '19 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.