[英]Magento : Fatal error: Call to a member function getMethodInstance() on a non-object
[英]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.