1

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()); ?>  
2

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.

2
  • Thanks Could you plz explain how? Aug 6 '15 at 8:39
  • 1
    I will not cover all options. If you update your question to include how your block gets created (layout xml, inline, …) then I will update my answer accordingly.
    – mam08ixo
    Aug 6 '15 at 8:51

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.