I am trying to add a simple line with "Only X Left" on the cart table summary on Magento 1.9.1 only if qty of the products is less then the threshold specified in setting just like it happen in product page. Any tip?
1 Answer
Where to find that block
For simple products the block that shows "Only X Left" is Mage_CatalogInventory_Block_Stockqty_
Default
and it uses the template cataloginventory/stockqty/default.phtml
. For configurable and grouped products there are separate block types besides "Default".
Unfortunately it retrieves the current product from the global registry, so it's not intended to be used for multiple products on one page.
I would extend the block in my own custom module and implement a setProduct()
method to allow using it with arbitrary products. But you could also set and unset the current_product
registry before and after rendering the block.
How to add it to the cart
The template you have to edit is checkout/cart/item/default.phtml
(see "How to change templates" in this answer for more info)
There you have access to the product model with $_item->getProduct()
.
An example how to render the default block with the registry trick mentioned above:
Mage::register('current_product', $_item->getProduct());
echo $this->getLayout()
->createBlock('cataloginventory/stockqty_default')
->setTemplate('cataloginventory/stockqty/default.phtml')
->toHtml();
Mage::unregister('current_product');