Track Inventory for Configurable Products

We recently had a client who needed to be able to manage inventory for configurable products in Magento. Since configurable products are intended to merely group simple products together, the ability to track inventory for configurable products is not something that is possible in a vanilla Magento install.

I dug into the Magento codebase to better understand how inventory is managed for the different product types in Magento. As you could probably guess, the CatalogInventory module handles all of Magento’s inventory management. In side of the /app/code/core/Mage/CatalogInventory/etc/config.xml file, you’ll find the following section of xml contained in the global tag:

<catalog>
<product>
        <type>
            <simple>
                <is_qty>1</is_qty>
            </simple>
            <virtual>
                <is_qty>1</is_qty>
            </virtual>
            <configurable>
                <stock_indexer>cataloginventory/indexer_stock_configurable</stock_indexer>
            </configurable>
            <grouped>
                <stock_indexer>cataloginventory/indexer_stock_grouped</stock_indexer>
            </grouped>
        </type>
    </product>
</catalog>

This section of xml is used by Mage_CatalogInventory_Helper_Data::getIsQtyTypeIds method to determine if a certain product type “qualifies” for inventory tracking. It is also used to indicate if there is a custom resource model that needs to be used to calculate the stock availability for a certain product type.

The beautiful thing about the way Magento handles the configuration xml is that we can create xml nodes that mirror the structure of the above xml in a config.xml file in a custom module – this allows us to change pretty much any configuration value without having to touch any of the config.xml files of core Magento modules. Magento will then merge the xml nodes from both config.xml files. Since custom modules are loaded after core modules, any nodes that override default Magento nodes will take precedence.

I created a small module that enables inventory tracking for configurable products. The following sections in Magento are overridden by this module:

  • app/code/core/Mage/CatalogInventory/Model/Observer.php (lines 365 – 405)
  • app/code/core/Mage/CatalogInventory/etc/config.xml (line 202)
  • app/design/adminhtml/default/default/template/catalog/product/tab/inventory.phtml (line 55)


Note:

  • There are two different ways of tracking inventory for configurable products. Read the comments in the app/code/local/CLS/ConfigurableInventory/etc/config.xml file for details.
  • This module is built for Magento 1.4.0.1. It’ll likely work on older versions of Magento, but I’ve only tested it on 1.4.0.1

UPDATE (5/12/10): After working with this test module on a dev site, it looks like the $item->getProduct() method call on line 21 in the CLS_ConfigurableInventory_Model_CatalogInventory_Observer class actually isn’t returning a product model for all order items. This is something that I’ll be debugging once we actually implement this test code in a project. I’ll try to post an update here when we do that.

I’d love to hear from any of you that end up implementing this code on your site.

Share it

Topics

Related Posts

Everything You Need to Know About Updating to Google Analytics 4

Classy Llama is Proud to be a Sponsor of Signifyd’s #FLOWSummit2023

Classy Llama Earns 2 New Workplace Awards from Comparably.com

Contact Us