Magento – Prevent the quantity of inventory in the backend products is negative

inventorymagento-1stock

I'm using Magento 1.7.0.2 and Magento 1.9.1.0.

As we know, by default, Qty Inventory at the backend (Admin – Catalog – Manage Products – Edit – General – Inventory – Qty) may contain a negative value, such as -1.

How to prevent the quantity of inventory in the backend products is negative?

Best Answer

You could create a custom module and an event observer hooked to the cataloginventory_stock_item_save_before event then you can validate if the qty will be saved as negative and perform some action.

Configuration:

Vendor/Module/etc/config.xml

<global>
...
    <events>
        <cataloginventory_stock_item_save_before>
            <observers>
                <vendor_module_stock_item_save_before>
                    <class>vemdor_module/observer</class>
                    <method>stockItemSaveBefore</method>
                </vendor_module_stock_item_save_before>
            </observers>
        </cataloginventory_stock_item_save_before>
    </events>
</global>
...

Observer:

Vendor/Module/Model/Observer.php

class Vendor_Module_Model_Observer
{
   public function stockItemSaveBefore(Varien_Event_Observer $observer)
   {
        $item = $observer->getEvent()->getItem();
        // Perform some action ...
   }
}

I don't know what do you want to do with that information. You may want to send an email, save a log or just keep the qty in 0, regardless the action you'll take this is the way to do it.