Reputation: 33
So I have a cms block in this template that is on the side of each product listing.
I have this code in the CMS block:
{{block type="core/template" name="some_unique_name" template="myfolder/my_dynamic_php_content.phtml"}}
this in my_dynamic_php_content.phtml:
<?php echo $_product->getData('product_brand'); ?>
and i get the error:
Call to a member function getData() on a non-object in /home/magento/public_html/shorepowerinc.com/app/design/frontend/fortis/default/template/myfolder/my_dynamic_php_content.phtml on line 1
I'm kind of a newbie, so if anyone could explain what's going on here, I would greatly appreciate it!
Upvotes: 2
Views: 4970
Reputation: 6777
Two things missing from this;
1) You need to load the model for the current product and
2) You've used the wrong syntax when displaying the attribute.
To load the current product model in your block add this to the top of your my_dynamic_php_content.phtml
template;
<?php
$_prodID = Mage::registry('current_product')->getId();
$_product = Mage::getModel('catalog/product')->load($_prodID );
?>
And then to output the attribute in the template;
<?php echo $_product->getProduct_brand() ?>
(note it's camelcase that's used, but you keep the underscore if you've used that when setting up your attribute).
EDIT
The above answer will work but the underscore will be handled by Varien_Object::_underscore()
and the prevailing convention is to use either $_product->getData('product_brand') or $_product->getProductBrand(). Thanks to Ben Marks in the comments below for this info.
Upvotes: 2