2

我正在尝试通过覆盖 Catalog/Product/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<1

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

1 回答 1

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

于 2012-11-06T20:03:49.787 回答