0

当我尝试使用以下代码从某个类别中获取 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
4

2 回答 2

1

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

$_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);
于 2013-09-15T13:50:30.277 回答
0

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

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

确保您具有带有此is_in_stock标识符的 in stock 属性,并且与qty相同

请注意,请用您在您的情况下使用的版本标记您的问题,它是 1.4 还是 1.7 ?另外magento官方支持Linux环境

于 2013-09-14T19:50:17.517 回答