[英]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我是 magento 的新手,最近开发了一个网站,但在搜索框中,如果我搜索任何内容,它会显示此消息
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致命错误:在第 32 行的/home/bjcprod2/public_html/app/design/frontend/amazon/default/template/catalog/product/list.phtml 中的非对象上调用成员函数getLevel()
<?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 是通过从注册表中加载当前类别来设置的:
$_category = Mage::registry('current_category');
On the next line, notice that it tests whether $_category has loaded an object before proceeding:在下一行,请注意它在继续之前测试 $_category 是否已加载对象:
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.稍后代码在 $_category 上调用 getLevel(),但在这种情况下,它不会首先测试 $_category 是否是一个对象。
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.您可以修改代码以检查 $_category 是否为对象。 Something like:就像是:
if($_category && $_category->getLevel() == 2)
{
include('category_'.$styletemplate.'.phtml');
} else {
include('list_'.$styletemplate.'.phtml');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.