My Problem might simple, but I am not getting where exactly problem is.
I have created on custom block under local cood pool. then extends the Mage_Catalog_Block_Product_Abstract
method, I have also created on phtml page under frontend > page > html
.
My code works perfect when I click on product page. But when I move to index page It gives me error,
Call to a member function getName() on a non-object-
Please refer following code and give me some suggestions.
Block
class Kultureshop_Page_Block_Html_Custom extends Mage_Catalog_Block_Product_Abstract
{
protected $_crumbs = null;
protected $_cacheKeyInfo = null;
public function __construct()
{
parent::__construct();
$this->setTemplate('page/html/custom.phtml');
}
public function getProduct()
{
if (!Mage::registry('product') && $this->getProductId())
{
$product = Mage::getModel('catalog/product')->load($this->getProductId());
Mage::register('product', $product);
}
return Mage::registry('product');
}
}
phtml
$_product = $this->getProduct(); ?>
<div class="">
<h1><b> <?php echo($_product->getName()); ?> By <?php echo($_product->getArtist()); ?>
Best Answer
When entering the index page, there is neither a registry entry available nor a product ID passed to your custom block.
On the product page, the current product is registered via
Mage_Catalog_Helper_Product::initProduct()
, so it works fine there.On the index page, you need to pass in a product ID explicitly when creating your custom block. There are multiple options to do so, depending on how you declare/create the block.