cancel
Showing results for 
Search instead for 
Did you mean: 

Viewing product shows Call to a member function getId() on a non-object

Viewing product shows Call to a member function getId() on a non-object

When I go to view a product, I am seen with this error:

 

 

Fatal error: Call to a member function getId() on a non-object in /chroot/home/mpgautop/mpgautoparts.com/html/app/code/core/Mage/Catalog/Block/Category/View.php on line 43

 

 

Here are lines 42-47:

 

        if ($headBlock = $this->getLayout()->getBlock('head')) {
			$cat_id = $this->getCurrentCategory()->getId();
			$category = Mage::getModel('catalog/category')->getCurrentCategory()->getId();            
            if ($title = $category->getMetaTitle()) {
                $headBlock->setTitle($title);
            }

Does someone know of a fix for this? As I keep getting errors for line 43 and 44.

1 REPLY

Re: Viewing product shows Call to a member function getId() on a non-object

Hi,

You could rewrite the code as follows which will get around the error.. hopefully it still performs as you expect it too:

 

if ($headBlock = $this->getLayout()->getBlock('head')) {
    $currentCategory = $this->getCurrentCategory();
    if($currentCategory) {
        $cat_id = $currentCategory->getId();
        $category = Mage::getModel('catalog/category')->load($cat_id);           
        if ($title = $category->getMetaTitle()) {
            $headBlock->setTitle($title);
        }
    } else {
        // Cannot locate current category... 
// Add some fallback code here?
// e.g. $headBlock->setTitle('Default Title'); } }
Problem solved? Click Accept as Solution!
Magento Certified Developer Plus | www.iwebsolutions.co.uk | Magento Small Business Partner