-1

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.

suresh
  • 1
  • 2
  • 1
    Did you go to line **32** on `list.phtml` to see what's being called and why? – al'ein Sep 11 '15 at 10:51
  • Post some of your code that you have developed so far, so we can see what can be going on. – al'ein Sep 11 '15 at 10:52
  • Magneto is a mutant supervillain - please fix the typo in your question heading. It is important as a new user to take a good look at http://stackoverflow.com/help/how-to-ask – micstr Sep 11 '15 at 10:56
  • @suresh edit your question and post your code there, instead of inside comments. – al'ein Sep 11 '15 at 10:57

1 Answers1

0

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');
}
  • I tried sir and the error is gone but it shows the empty page.not displayed any products even that exist. – suresh Sep 14 '15 at 05:05
  • If there is no category object available, how is your product collection being set? Your code has: `$_productCollection=$this->getLoadedProductCollection();`, so whether products show depends on what `$this` is (the block) and how its `$getLoadedProductCollection()` function is written. – BonnevilleSlim Sep 14 '15 at 16:54