正如您可能猜到的,当我们启用Flat Catalog Product
选项时,我们无法从产品报告集合中获取“名称、价格等”等产品属性。启用此选项后,所有属性都保留在catalog_product_flat
中。因此,我想进行扩展以显示浏览次数最多的产品,但由于上述问题而没有成功。
我尝试了很多方法,但没有发生如下:
$collection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect(*)
->setStoreId($store)
->addStoreFilter($store)
->addViewsCount();
$collection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect(*)
->addAttributeToSelect(array('name', 'price')) // this will not work is because of the collection
->setStoreId($store)
->addStoreFilter($store)
->addViewsCount();
那么,您有什么想法获得上述收藏吗?
最佳答案
使用catalog_product_flat_$storeId进行内部连接应该可以解决问题:
$storeId = Mage::app()->getStore()->getId();
$collection = Mage::getResourceModel('reports/product_collection')
->addViewsCount();
$collection->getSelect()->joinInner(array('e2' => 'catalog_product_flat_'.$storeId), 'e2.entity_id = e.entity_id');
foreach ($collection as $prod) {
echo "Name: ".$prod->getName()."\n";
echo "Price: ".$prod->getPrice()."\n";
echo "Views: ".$prod->getViews()."\n";
echo "\n";
}
不过,似乎无法按属性过滤。这会因 join 和 addViewsCount() 调用而变得困惑。它将属性添加到选择查询中,但也选择 *。
关于magento - 平面产品问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8927870/