doufen5175 2013-03-25 14:16
浏览 49
已采纳

按价格列出Magento产品,但最终价格为零

Is there a way in the product search sorting list to order the products by highest price to lowest price but then have products with zero price displayed at the end.

The normal sorting works fine (low to high or high to low) but would really like the ability to include the zero price products at the end, an example of how it would be listed would be:

Product 1: £3

Product 2: £18

Product 3: £0

Product 4: £0

I haven't found a way from searching here or Google yet and not being too familiar with Magento I am not sure where I would look to change this. If anyone has an answer or can point me to the query or correct file for me to edit myself I don't mind having a go myself.

Using some help from here and other questions I have edited the _getProductCollection() method inside the file /app/code/core/Mage/Catalog/Block/Product/List.php (don't worry I've copied to local) and added these few lines:

$orderFilterType = $this->getRequest()->getParam('order');
$dirFilterType = $this->getRequest()->getParam('dir');

if( isset( $orderFilterType ) && $orderFilterType == 'price' && isset( $dirFilterType ) && $dirFilterType == 'asc' ) {

$this->_productCollection = $layer->getProductCollection()->addAttributeToSort( 'price', 'DESC' );


} else { 

$this->_productCollection = $layer->getProductCollection();

}

This means that whatever I do inside this code will only run once someone has selected the orderby price dropdown with the ascending option.

The problem is now is I'm not sure what to do to affect the value for $this->_productCollection = $layer->getProductCollection(); to make this return as I want.

  • 写回答

4条回答 默认 最新

  • douran9707 2017-04-20 15:52
    关注

    4 years late to the party I know but I've just solved this in a much nicer way than the other solutions I found, might help someone.

    In Catalog/Block/Product/List/Toolbar.php replace

    $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
    

    with

    $zeroPriceLast = new Zend_Db_Expr('`price_index`.`price` = 0 ASC, `price_index`.`price`  ASC');
    
    $this->_collection->getSelect()->order($zeroPriceLast);
    

    and wrap in a conditional so that the new logic is only applied when sorting by price.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路