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)
You can download this module here. All you need to do to install the module is to copy the app folder into your Magento install folder. 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.
This entry was posted on Saturday, May 8th, 2010 at 1:25 pm and is filed under Magento Development. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
June 4th, 2010 at 8:34 am
Thanks for this. I am going to use this for a Grouped product since it has the same issues as Configurable. So far, I have it installed and am seeing the ability to set inventory on my grouped product. Saved me a ton of time! Have you come up with anything yet for the bug above from 5/12/10?
June 4th, 2010 at 8:39 am
@Brett – No, we have not yet implemented this on a project, so we’ve not spent the time resolving that bug. I would recommend adding some products from your grouped product to the cart and going through the checkout process to see if you have any errors.
June 17th, 2010 at 1:51 pm
I’m unable to extract the file. What the type and utility? I’d love to be able to try this out.
June 17th, 2010 at 3:01 pm
@Gfxguru – You should be able to find an app for your OS by Google-searching “Extract tgz on NAME OF OS”
November 5th, 2010 at 12:34 am
Me trying to implement this code in Magento Enterprise Edition ver. 1.9.0.0. Jst started let me know if you have more suggestion/guide for me.
Thanks a lot in advance…
November 5th, 2010 at 11:29 am
@Satya – We did run into some things that had changed in later version of Magento (1.3.2.4 is what this code is written against) that caused us to have to change our implementation. You’ll want to study the code that this affects to see what you’ll need to do to get this to work on EE 1.9