繁体   English   中英

致命错误:在 magento 中的非对象上调用成员函数 getLevel()

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

我是 magento 的新手,最近开发了一个网站,但在搜索框中,如果我搜索任何内容,它会显示此消息

致命错误:在第 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');}
?>

请帮忙。

提前致谢。

在发布的代码中, $_category 是通过从注册表中加载当前类别来设置的:

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

在下一行,请注意它在继续之前测试 $_category 是否已加载对象:

if($_category) {. . .

稍后代码在 $_category 上调用 getLevel(),但在这种情况下,它不会首先测试 $_category 是否是一个对象。

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

这允许您发生错误。

您可以修改代码以检查 $_category 是否为对象。 就像是:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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