【问题标题】:Magento sort bestseller hide unsold products issueMagento 排序畅销书隐藏未售出的产品问题
【发布时间】:2018-11-01 09:53:23
【问题描述】:

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

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

代码看起来运行良好,但我只需要在用户点击该过滤器时显示已售商品

/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;
    }

是否可以对只有销售额的产品进行筛选和排序,或者我需要更改一些其他功能,谢谢

【问题讨论】:

    标签: php magento magento-1.9


    【解决方案1】:

    您可以使用~/lib/Zend/Db/Select.php 中的having() 函数仅返回集合中具有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') 看看是否有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多