Magento自定义排序选项

Anu*_*rma 4 magento magento-1.7

如何在Magento中添加自定义排序选项.除了按价格排序外,我想添加畅销商品,评分最高和独家.请帮忙

liy*_*kat 8

对于畅销书

code/local/Mage/Catalog/Block/Product/List/Toolbar.php方法setCollection中使用了

public function setCollection($collection) {
    parent::setCollection($collection);
    if ($this->getCurrentOrder()) {
        if($this->getCurrentOrder() == 'saleability') {
            $this->getCollection()->getSelect()
                 ->joinLeft('sales_flat_order_item AS sfoi', 'e.entity_id = sfoi.product_id', 'SUM(sfoi.qty_ordered) AS ordered_qty')
                 ->group('e.entity_id')->order('ordered_qty' . $this->getCurrentDirectionReverse());
        } else {
            $this->getCollection()
                 ->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        }
    }

    return $this;
}
Run Code Online (Sandbox Code Playgroud)

在setCollection之后我添加了这个方法:

public function getCurrentDirectionReverse() {
    if ($this->getCurrentDirection() == 'asc') {
        return 'desc';
    } elseif ($this->getCurrentDirection() == 'desc') {
        return 'asc';
    } else {
        return $this->getCurrentDirection();
    }
}
Run Code Online (Sandbox Code Playgroud)

最后我将mehod setDefaultOrder更改为

public function setDefaultOrder($field) {
    if (isset($this->_availableOrder[$field])) {
        $this->_availableOrder = array(
            'name'        => $this->__('Name'),
            'price'       => $this->__('Price'),
            'position'    => $this->__('Position'),
            'saleability' => $this->__('Saleability'),
        );
        $this->_orderField = $field;
    }

    return $this;
}
Run Code Online (Sandbox Code Playgroud)

评分最高

http://www.fontis.com.au/blog/magento/sort-products-rating

尝试上面的代码.

添加日期

Magento - 按添加日期排序

对于任何工作或关注,我不与任何上述链接相关联,只是出于知识目的和解决您的问题.

希望这一定会对你有所帮助.