1

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?

  • where you are using this helper function ? – liyakat May 28 '15 at 10:36
  • Just updated the question – Ronny May 28 '15 at 10:39
3

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;
    }
| improve this answer | |
  • I used your code, but now get a Fatal error on the 'getType' from line 4 – Ronny May 28 '15 at 11:11
  • 1
    sorry,it was mistake.it should be getTypeId – Amit Bera May 28 '15 at 11:12
  • Still get a Error PHP Fatal error: Call to a member function getTypeId() on a non-object but should the $productObject not be $_product? – Ronny May 28 '15 at 11:18
  • i listed the full code .... (without the extra html marks) – Ronny May 28 '15 at 11:36
  • 1
    i will check... on tomorrow – Amit Bera May 28 '15 at 17:06
1

you can pass product id as params to your helper like below

if you are override catalog data helper or you can add your own helper instead in your

app\design\frontend\customtheme\default\template\catalog\product\list.phtml

Mage::helper('catalog')->getAvailableAttributesHelper($_product->getId());

and your function will be like

public function getAvailableAttributesHelper($productId) {

$_product   = Mage::getModel('catalog/product')->load($productId);
.....

hope this will work for you.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.