0

In Magento 1.7, I want to do like after credit memo create product quantity should be deduct.

How to do that?

Any help would be appreciated.

Thanks.

1

You can use sales_order_invoice_save_after to deduct product quantity after generate credit memo :

Create config.xml for define event and module :

app/code/local/RH/StockDeduct/etc/config.xml

<?xml version="1.0"?>

<config>
    <modules>
        <RH_StockDeduct>
            <version>1.0.0</version>
        </RH_StockDeduct>
    </modules>
    <adminhtml>
        <events>
            <sales_order_invoice_save_after>
                <observers>
                    <RH_StockDeduct_order_invoice_save_after>
                        <type>singleton</type>
                        <class>RH_StockDeduct_Model_Observer</class>
                        <method>decreaseqty</method>
                    </RH_StockDeduct_order_invoice_save_after>
                </observers>
            </sales_order_invoice_save_after>
        </events>
    </adminhtml>
</config>

Create Observer.php file for add code for deduct qty after credit memo generate :

app/code/local/RH/StockDeduct/Model/Observer.php

<?php
class RH_StockDeduct_Model_Observer {
    public function decreaseqty(Varien_Event_Observer $observer) {
        try {
            $invoice = $observer->getEvent()->getInvoice();
            $invoiceItems = $invoice->getAllItems();
            foreach ($invoiceItems as $item) {
                $productId = $item->getProductId();
                $product = Mage::getModel('catalog/product')->load($productId);
                $stockItem = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
                $sku = $product->getSku();
                $qty = $stockItem - $item->getQty();
                $product->getStockItem()->setData('qty', $qty)->save();
            }
            return $this;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }
}
| improve this answer | |
  • Thanks !! It's working like charm !! – Niket Dec 3 '19 at 2:32

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.