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条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题