abstract.phtml上的致命错误,试图获取仅库存产品的产品集合


Fatal error on abstract.phtml, trying to get a product collection of products in stock only

当我尝试使用以下代码从某个类别中获取 4 种产品库存时

 $_helper = $this->helper('catalog/output');
 $_category = Mage::getModel('catalog/category')->load($this->getCategoryId());
 $_productCollection = Mage::getResourceModel('reports/product_collection')
                       ->addAttributeToSelect('*')
->addAttributeToFilter('is_in_stock', 1)
->addAttributeToFilter('qty', ">1")
                       ->addCategoryFilter($_category)
                       ->setVisibility(array(2,3,4));
 $_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));                  
 $_productCollection->setPage(1, 4);

我收到以下异常

Fatal error: Call to a member function getBackend() on a non-object in /home/xxx/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 816

尝试像这样获取产品集合:

$_productCollection = Mage::getResourceModel('reports/product_collection')
                       ->addAttributeToSelect('*')
                       ->addCategoryFilter($_category)
                       ->setVisibility(array(2,3,4));
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($_productCollection); //this should filter in stock products only
$_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));                  
$_productCollection->setPage(1, 4);

此错误通常是由于调用错误的模型,或对该集合中不存在的属性应用过滤器 仔细检查产品属性 ID 是否存在于您当前的 magento 转到管理员 检查您的属性并将键与

->addAttributeToFilter('is_in_stock', 1)
->addAttributeToFilter('qty', ">1")

确保您具有具有此is_in_stock标识符的库存属性,并且数量相同

注意,请使用您在您使用的版本标记您的问题 如果是 1.4 或 1.7 ?Magento也正式支持Linux环境