Fix ‘Call to a Member Function getId() on a Non-Object’ Error in PHP

helperPHP

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 .This registry 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 :

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)) 
    {
        foreach($attributes as $att)
        { $pAtt=$_product->getProductAttribute();
         //get the child products
             $allProducts = $_product->getTypeInstance(true)->getUsedProducts(null, $_product);
                $frontValues =array();
                 //<li>
                     $pAtt->getFrontendLabel();}
                 // CODE NOT FINISHED YET ...
        }
      }
      .....
}

Update code like:

    public function getAvailableAttributesHelper($_product){
        $html='';
            if($_product->getTypeId()=='configurable'):
                $AllowAttributes=$_product->getTypeInstance(true)->getConfigurableAttributes($_product);
                     $skipSaleableCheck = Mage::helper('catalog/product')->getSkipSaleableCheck();
                    $allProducts = $_product->getTypeInstance(true)
                                    ->getUsedProducts(null, $_product);
                    $option=array();
                    foreach ($allProducts as $product) {
// only show available option
                if(!$product->isSaleable()) continue;
                                foreach ($AllowAttributes as $attribute) {
                                        $productAttribute   = $attribute->getProductAttribute();
                                        $productAttributeId = $productAttribute->getId();
                                            $option[$productAttribute->getAttributeCode()][]=$productAttribute->getFrontend()->getValue($product);
                                }
                    }


                    $html='<ul>';
                    foreach($option  as $key=>$eachoption){
                            //array_unique($eachoption);
                            $html.='<li>'.$key.'<ul>';  
                            //print_r(array_unique($eachoption));
                                foreach(array_unique($eachoption) as $value){
                                    $html=$html.'<li>'.$value.'</li>';

                            }
                            $html.='</ul></li>';
                    }
                    $html.='</ul>';

            endif;
        return $html;
    }
Related Topic