0

I am trying to pull my attributes inside sidebar with the following code:

$attributeValue = Mage::getModel('catalog/product')
        ->load($_product->getId())
        ->getAttributeText('weight_category');
echo $attributeValue;

But I get this error:

Fatal error: Call to a member function getId() on a non-object in 

Any idea?

UPDATE (from srdan)

I work with Josip on that project. The problem is that everytime we want do something with $_product object the error:

Fatal error: Call to a member function getId() on a non-object in /path/to/template/where/code/is.phtml

shows up. We use code below and error above shows up on frontend:

<?php
    $_helper = $this->helper('catalog/output');
    $_product = $this->getProduct();
?>

<?php if($_product->getSidebar()): ?>
<p>Attribute: <?php echo $_helper->productAttribute($_product, $_product->getSidebar(), 'price') ?></p>
<?php else: ?>
<p>No Attribute text here!</p>
<?php endif; ?> 
  • This means your $_product variable is not a valid object. Where exactly are you executing your code? – user487772 Feb 12 '13 at 21:03
  • In left.phtml which is for left sidebar.. – Josip Gvozdić Feb 12 '13 at 21:11
  • 2
    catalog/navigation/left.phtml only serves catalog layered navigation. What are you trying to achieve? – user487772 Feb 12 '13 at 21:27
  • This doesn't look like a duplicate to me. The other question includes different code and describes a different behaviour. – Adam Lear Feb 14 '13 at 21:33
  • Was your question answered? Then please mark a solution. – Fabian Blechschmidt Feb 28 '13 at 17:44
1

It is hard to give an answer to a vague question but I think that you are misunderstanding Magento layout structure.

catalog/navigation/left.phtml is used by <block type="catalog/navigation" .. />, which means it is an instance of Mage_Catalog_Block_Navigation class. This class doesn't have neither getProduct() method, nor product property. So in your code $_product variable equals to null and has no getId() method.

This was an anthomology of your error message. Now how you shall do it correctly. Assuming that you want to put a new sidebar block to the left column of your products details page you should do the following:

  1. Add a block to the layout of your products details page. To do so you have to add the following to local.xml file of your theme:

    <catalog_product_view>
        ...
        <reference name="left">
            <block type="core/template" name="your.custom.sidebar.block" template="callouts/your-custom-sidebar-block.phtml"/>
        </reference>
        ...
    </catalog_product_view>
    
  2. In your theme create callouts/your-custom-sidebar-block.phtml template where you can access the current product object like Fabian already told you:

    $_product = Mage::registry('current_product');
    
  • We added this in local.xml: <catalog_product_view> <reference name="left"> <block type="core/template" name="attributes.sidebar" template="callouts/attributes-sidebar.phtml"/> </reference> </catalog_product_view> – Josip Gvozdić Feb 13 '13 at 11:02
  • and? does it show up anything at products details page? – user487772 Feb 13 '13 at 11:04
  • And this in /app/frontend/default/our_theme/template/callouts/attributes-sidebar.phtml: <?php $_helper = Mage::helper('catalog/category'); $_product = Mage::registry('current_product'); ?> <?php if($_product->getSidebar()): ?> <p>Attribute: <?php echo $_helper->productAttribute($_product, $_product->getSidebar(), 'price') ?></p> <?php else: ?> <p>No Attribute text here!</p> <?php endif; ?> – Josip Gvozdić Feb 13 '13 at 11:04
  • Nothing appears. – Josip Gvozdić Feb 13 '13 at 11:05
  • But no error anymore? What is $_product->getSidebar()? Do you have an attribute called sidebar or what? – user487772 Feb 13 '13 at 11:06
1

Depending on what you try to achieve... Normally you can get the current product via:

Mage::registry('current_product')
  • Tried that, I get same error. – Josip Gvozdić Feb 13 '13 at 10:08
  • what did you try? – Fabian Blechschmidt Feb 13 '13 at 10:08
  • <?php $_helper = $this->helper('catalog/output'); $_product = Mage::registry('current_product'); ?> <?php if($_product->getSidebar()): ?> <p>Attribute: <?php echo $_helper->productAttribute($_product, $_product->getSidebar(), 'price') ?></p> <?php else: ?> <p>No Attribute text here!</p> <?php endif; ?> – Josip Gvozdić Feb 13 '13 at 10:15
  • can you check: echo get_class($this); and echo get_class(Mage::registry('current_product')); – Fabian Blechschmidt Feb 13 '13 at 12:13
  • We tried to echo some text but it isnt workin, so I supose this wont either. – Josip Gvozdić Feb 13 '13 at 18:14

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.