简体   繁体   中英

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

I am new to magento,recently developed one site but in search box if i search anything it shows this message

Fatal error: Call to a member function getLevel() on a non-object in /home/bjcprod2/public_html/app/design/frontend/amazon/default/template/catalog/product/list.phtml on line 32


 <?php
    $productOnLine = 4;
    $_productCollection=$this->getLoadedProductCollection();
    $_collectionSize = $_productCollection->count();
?>
<?php 
$_category = Mage::registry('current_category');
if($_category) {
    $id = $_category->getId();
    $name = $_category->getName();
    //Zend_Debug::dump($id);
    $styletemplate = (int)$_category->getStyletemplate();   //echo $styletemplate;
    $totalPerPage = ($this->show_total) ? $this->show_total :4;
    $counter = 1;
    $visibility = array(
            Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
            Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
    );
    $storeId = Mage::app()->getStore()->getId();

    $_productCollection1 = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addOrderedQty()
    ->addAttributeToFilter('visibility', $visibility)
    ->setStoreId($storeId)
    ->addCategoryFilter(Mage::getModel('catalog/category')->load($id))
    ->setOrder('ordered_qty', 'desc');
    $categoryc = Mage::getModel('catalog/category')->load($id);
    $children = explode(',', $categoryc->getChildren());
}
if($_category->getLevel() == 2){    include('category_'.$styletemplate.'.phtml');   }else{  include('list_'.$styletemplate.'.phtml');}
?>

please help.

Thanks in advance.

In the posted code, $_category is set by loading the current category out of the registry:

$_category = Mage::registry('current_category');

On the next line, notice that it tests whether $_category has loaded an object before proceeding:

if($_category) {. . .

Later the code calls getLevel() on $_category, but in this case it does not test first to see if $_category is an object.

if($_category->getLevel() == 2). . .

This allows the error you are getting to occur.

You could modify the code to check that $_category is an object. Something like:

if($_category && $_category->getLevel() == 2)
{
    include('category_'.$styletemplate.'.phtml');
} else {
    include('list_'.$styletemplate.'.phtml');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM