i am making a helper file. But i get a Fatal Error at the beginning:
PHP Fatal error: Call to a member function getId() on a non-object in /pathtohelper/Helper/Data.php on line 2
My code:
public function getAvailableAttributesHelper($_product) {
$_product = Mage::getModel('catalog/product')->load($product_id);
if ($_product->getTypeId()=='configurable')
{
//Get Attributes
$attributes = $_product->getTypeInstance(true)->getConfigurableAttributes($_product);
if (count($attributes))
{
$html .= '<ul>';
foreach($attributes as $att)
{ $pAtt=$_product->getProductAttribute();
//get the child products
$allProducts = $_product->getTypeInstance(true)->getUsedProducts(null, $_product);
$frontValues =array();
$html .= '<ul>';
$pAtt->getFrontendLabel();
$html .= '<ul>';
foreach($allProducts as $p){
//check stock, status, ...
//do not show unsaleable options
if(!$p->isSaleable()) continue;
$out=$p->getAttributeText($pAtt->getName());
$frontValues[$out]=$out;
}
$html .= '<ul>';
implode('</li><li>', $frontValues);
$html .= '</li></ul></li>';}
}
$html .= '</ul>';
}
}
I have Magento 1.8.1 CE.
I also tried to replace line 2 en 3 with:
$_product=Mage::getModel('catalog/product')->load($_product->getId());
But with the same result.
The helper is from a custom theme.
Which is being used in
app\design\frontend\customtheme\default\template\catalog\product\list.phtml
What am i doing wrong?
Best Answer
Mage::registry('current_product') only work on
product details
page as per as default magento .Thisregistry variable is only define at product view page
.if you,want to get product id then you need to send current product object at function
getAvailableAttributesHelper.
Just like
Mage::helper('helper_Prefix')->getAvailableAttributesHelper($_product)
.Also it is very bad idea to
load full product object ($_product=Mage::getModel('catalog/product')->load()
at list page.It create issue in speed.Code may be like :
Update code like: