Magento – Add the “Only x Left” in product list page

product-liststock-status

I have a custom theme template for my magento 1.9.1 and would like to add the "only X left" in my product list page. It is already in my product view page however I want it to show on every single product in the grid list page.
How do I add to it?

Best Answer

Rather than instantiating the model for each product rather do this.

Create a magento module, and add these bits to it.

In the module config.xml

<frontend>
    <events>
        <catalog_product_collection_load_after>
            <class>your_module/observer</class>
            <method>addStockItemData</method>
        </catalog_product_collection_load_after>
    </events>
</frontend>

In the module observer

<?php 
// observer
public function addStockItemData($observer)
{
    $collection = $observer->getCollection();
    Mage::getSingleton('cataloginventory/stock')->addItemsToProducts($collection);
}

That will do the trick

Related Topic