我有一个代码来展示我们商店中最畅销的产品。如果我禁用扁平类别和扁平产品,代码效果很好。有没有办法在不禁用扁平产品和类别的情况下完成这项工作。感谢...
class Mage_Catalog_Block_Product_Viewed extends Mage_Catalog_Block_Product_Abstract{
public function __construct(){
parent::__construct();
$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addMinimalPrice()
->addAttributeToSelect('*')
->addAttributeToSelect(array('name', 'price', 'small_image'))
->setStoreId($storeId)
->addStoreFilter($storeId)
->setOrder(‘ordered_qty’, ‘desc’);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
$products->setPageSize(15)->setCurPage(1);
$this->setProductCollection($products);
}
}
答案 0 :(得分:0)
请确保您的查询需要返回的所有产品属性都标记为"用于产品列表" =在Magento管理员中为YES(目录>管理属性)。设置此(或其他一些)选项时,属性仅成为Flat表的一部分,这是启用平面目录时的主要区别之一。
您可以使用Mage :: log()查询集合选择,并查看与平面目录的不同之处,这有助于您了解差异并进行调试:
$products->setPageSize(15)->setCurPage(1);
$products->load(); // this is needed only for debugging,
// to get the "real" database query
// PLEASE remove this and Mage::log
// in production as it would affect performance
Mage::log( (string)$products->getSelect() );
$this->setProductCollection($products);