magento:根据当前的产品系列获得最畅销的产品

时间:2012-11-06 19:54:32

标签: magento

我试图通过覆盖目录/产品/ List.php来检索使用当前产品系列的畅销产品

<?php
 if($this->getRequest()->getParam('best')==1){ 

     $this->_productCollection->getSelect()
          ->joinLeft(array('items'=>'sales_flat_order_item'), "items.product_id = e.entity_id", array('count'=>'SUM(qty_ordered)'))
          ->group('e.entity_id');
     $this->_productCollection->getSelect()->having('SUM(items.qty_ordered) > ?',0);
     return $this->_productCollection;
 }
?>

使用上面的代码我收到以下SQL错误,错误是由于having子句引起的, 我如何在上面的代码中使用havinh子句来避免产生qty_ordered&lt; 1

的行
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'items.qty_ordered' in 'having clause'

1 个答案:

答案 0 :(得分:10)

如果您只想获得最畅销的产品

$storeId    = Mage::app()->getStore()->getId();

$products = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty()
            ->addAttributeToSelect(array('name', 'price', 'small_image')) //edit to suit tastes
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->setOrder('ordered_qty', 'desc'); //best sellers on top

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

print_r($products->getData());

查看更多@ http://magentocommerce.com/boards/viewthread/14764