2013-04-09 109 views
2

有几个案例报告说,这个查询的标准解决方案不起作用(SO上的类似问题正在束手无策,没有确切的解决方案)。Magento 1.7按订购数量排序 - 畅销书产品问题

什么人会做检索的“畅销书”的集合就是用这种查询生成器:

$storeId = Mage::app()->getStore()->getId(); 
    $collection = Mage::getResourceModel('reports/product_collection'); 

    $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds()); 

    $collection = $collection 
     ->addFieldToFilter('*') 
     ->addOrderedQty() 
     ->addStoreFilter() 
     ->setStoreId($storeId) 
     ->setOrder('ordered_qty', 'desc') 
     ->setPageSize(10) 
     ->setCurPage(1); 

现在,$collection结果持有一些虚假的价值和它的完全丢失了重要的属性(没有名字,价格等)。我甚至不能在1.7中尝试this core-code workaround

任何人都可以请发布一个解决方案,是Magento 1.7验证/认证/测试? (或最新版本的Magento)。

回答

2

这可能或可能不适合你,它可能不是最有效的方式,但它为我完成工作,因此它在这里与你的代码的一部分合并。

$storeId = Mage::app()->getStore()->getId(); 
$collection = Mage::getResourceModel('reports/product_collection'); 
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds()); 

$collection = $collection 
    ->addFieldToFilter('*') 
    ->addStoreFilter() 
    ->setStoreId($storeId) 
    ->setPageSize(10) 
    ->setCurPage(1); 

$collection->getSelect() 
    ->joinLeft(
     'sales_flat_order_item AS order_item', 
     'e.entity_id = order_item.product_id', 
     'SUM(order_item.qty_ordered) AS ordered_qty') 
    ->group('e.entity_id') 
    ->order('ordered_qty DESC') 
; 
+0

感谢您的回答,我目前有另一种实施依赖更多的程序方法。它并不优雅,所以我会给你的版本一个镜头。 – teodron 2013-06-30 09:00:20