Magento排序畅销书隐藏未售出的产品

时间:2018-11-01 09:53:23

标签: php magento magento-1.9

我正在研究magento自定义代码,该代码添加了功能以按销售数量对产品列表进行排序,

我为此使用了此网站代码 https://inchoo.net/magento/magento-products/sort-show-products-by-sold-quantity-in-magento/comment-page-1/

enter image description here

代码似乎可以正常工作,但是当用户点击该过滤器时,我只需要显示已售商品

/app/code/local/Inchoo/Catalog/Block/Product/List/Toolbar.php上的代码

public function setCollection($collection)
    {
        $this->_collection = $collection;

        $this->_collection->setCurPage($this->getCurrentPage());

        // we need to set pagination only if passed value integer and more that 0
        $limit = (int)$this->getLimit();
        if ($limit) {
            $this->_collection->setPageSize($limit);
        }
        if ($this->getCurrentOrder()) {


               if($this->getCurrentOrder() == 'qty_ordered') {
                $this->getCollection()->getSelect()
                     ->joinLeft(
                            array('sfoi' => $collection->getResource()->getTable('sales/order_item')),
                             'e.entity_id = sfoi.product_id',
                             array('qty_ordered' => 'SUM(sfoi.qty_ordered)')
                         )
                     ->group('e.entity_id')
                     ->order('qty_ordered ' . $this->getCurrentDirection());
            }

            else{           
            $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());           
            }
        }
        return $this;
    }

是否可以对仅销售的产品进行过滤和排序,或者我是否需要更改其他功能,谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用having()中的~/lib/Zend/Db/Select.php函数来仅返回集合中具有count(*) > 0的项目:

$this->getCollection()->getSelect()
    ->joinLeft(
        array('sfoi' => $collection->getResource()->getTable('sales/order_item')),
         'e.entity_id = sfoi.product_id',
         array('qty_ordered' => 'SUM(sfoi.qty_ordered)')
    )
    ->group('e.entity_id')
    ->having('qty_orderd > 0')
    ->order('qty_ordered ' . $this->getCurrentDirection());

这未经测试,有时需要表的别名,因此如果失败,请尝试->having('sfoi.qty_orderd > 0')->having('SUM(sfoi.qty_orderd) > 0')并查看其是否有效。

相关问题