Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Bringing inventory status to template
 
koubas
Jr. Member
 
Total Posts:  20
Joined:  2007-10-31
Czech Republic, Hradec Kralove
 

Hi, I’d like the inventory status of the product (exact qty) to be shown in the product datail page. How do I pull that and other extra information from the model to the template? What I also need is, when the product is configurable (to make it easier, with only one configurable parameter - for exaample the color), that the list of available subproducts (color variations) with their inventory states will be displayed on the product detail page too… I sould probably accomplish it by creating some view helper…
Thanks for directing me to the right way…

 
Magento Community Magento Community
Magento Community
Magento Community
 
koubas
Jr. Member
 
Total Posts:  20
Joined:  2007-10-31
Czech Republic, Hradec Kralove
 

I’ve found that I’m not alone with similar requirement http://www.magentocommerce.com/boards/viewthread/3741/
I think that the inventory level should be available as a product attribute, but it’s not :(

 
Magento Community Magento Community
Magento Community
Magento Community
 
iguru
Guru
 
Avatar
Total Posts:  331
Joined:  2007-11-01
Děčín, Czech Republic
 

@Koubas na houby co, kdyz nikdo neporadi hmmm Ale myslenka je to zajimava. Nevim, ale budu o tom premyslet. Snad odpovi nekdo z tymu. Co preklady? wink

 
Magento Community Magento Community
Magento Community
Magento Community
 
koubas
Jr. Member
 
Total Posts:  20
Joined:  2007-10-31
Czech Republic, Hradec Kralove
 

No na houby to je, na preklady ted bohuzel neni cas, nastesti shop, na kterym ted taky delam, je v anglictine smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
LapinLove404
Jr. Member
 
Avatar
Total Posts:  21
Joined:  2007-11-04
 

In another Thread, Moshe point me to this one :
http://www.magentocommerce.com/boards/viewthread/3980/#t15045

Looks like the below code will do it (I still have to try this) :

<?=Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
koubas
Jr. Member
 
Total Posts:  20
Joined:  2007-10-31
Czech Republic, Hradec Kralove
 

Thanks. Actually I’ve pointed it out my self today… I’ve made a helper which does exactly what I described in the initial post. I can publish it here if somebody wants.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Rico_Neitzel
Enthusiast
 
Avatar
Total Posts:  857
Joined:  2007-09-05
Würzburg, Germany
 

donot ask smile put it in here ! grin kwl thx! rico

 
Magento Community Magento Community
Magento Community
Magento Community
 
koubas
Jr. Member
 
Total Posts:  20
Joined:  2007-10-31
Czech Republic, Hradec Kralove
 

It might have been written cleaner a bit, but | haven’t got much time yet to get familiar with Magento’s guts. The funny thing is, that the first version of this helper had been completed a day before Mage 0.8.17240 was released with completely rewriten catalog/products model, so the first helper version was completely useless smile

<?php

class Mage_Catalog_Helper_Product_Inventory extends Mage_Core_Helper_Abstract {

    
function getLevel(Mage_Catalog_Model_Product $product{
        
        $stockItem 
Mage::getModel('CatalogInventory/Stock_Item');
        
$stockItem->loadByProduct($product);
        return 
$stockItem->getQty(); 
        
    
}
    
    
function getLevels(Mage_Catalog_Model_Product $product$groupByAttrCode{

        $options 
= array();
        
$levels = array();
      
        
        
$productType $product->getTypeInstance();
        
/* @var $productType Mage_Catalog_Model_Product_Type_Configurable */
        
        /* populate groupBy attribute options */
        
foreach ($productType->getConfigurableAttributes() as $attribute{

            $productAttribute 
$attribute->getProductAttribute();

            if (
$productAttribute->getAttributeCode() == $groupByAttrCode{
            
                $groupByAttrId 
$productAttribute->getId();
            
                foreach (
$attribute->getPrices() as $value{

                    $options[$value[
'value_index']] $value['label'];
                
                
}
                
                
break;
                
            
}
        }
        

        
foreach($productType->getUsedProducts(Mage::app()->getStore()->getId()) as $usedProduct{
        
            
/* @var $usedProduct Mage_Catalog_Model_Product */

            
if (!$usedProduct->isSalable()) continue;
            
            
$config $usedProduct->getConfigurableSettings();
            
            foreach(
$config as $attribute{
                
                
if ($attribute['attribute_id'== $groupByAttrId{
                    
                    
if (!isset($levels[$options[$attribute['value_index']]])) {
                        $levels[$options[$attribute[
'value_index']]] 0.0;
                    
}

                    $levels[$options[$attribute[
'value_index']]] += $this->getLevel($usedProduct);
                    break;
                    
                
}
                
            }
            
        }
        
        
return $levels;        

    
}

}

put it to magento_path/app/code/core/Mage/Catalog/Helper/Product/Inventory.php (I know about the ‘community’ directory, but I don"t know how does it work yet)

Usage inside templates:

to get inventory level of simple product:

<?=$this->helper('catalog/product_inventory')->getLevel($_product)?>

to get inventory levels of products, associated with configurable product:

<? $levels $this->helper('catalog/product_inventory')->getLevels($_product'groupBy attribute code'?>
which returns array(’attribute option1’ => (int)’inventory level’, .....)

The groupBy parameter works as follows: In our store we sell t-shirts. Every t-shirt is configurable product with configurable color and size. In product detail we want to present inventory levels (as a nice ‘graph’) per each available size of the t-shirt. When one size is available in more colors, their inventory levels are added (not so accurate, but looks nice wink), so ve set the groupBy parameter to the code of the SIZE attribute.

 
Magento Community Magento Community
Magento Community
Magento Community
 
liotropi
Sr. Member
 
Total Posts:  182
Joined:  2008-02-21
Athens, Greece
 

thanks, Koubas

I can’t get it to work
shows error in app.php

any ideas?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top