3

我浏览了一些与排序相关的帖子,但没有提出所需的解决方案。

我目前正在使用以下代码对类别视图字段中的产品进行排序,以覆盖默认选项并按toolbar.phtml 中的属性进行排序。

<option value="<?php echo $this->getOrderUrl('name', 'asc') ?>"<?php if($this->isOrderCurrent('name')): ?> selected="selected"<?php endif; ?>>
                 NAME
            </option>
            <option value="<?php echo $this->getOrderUrl('short_description', 'asc') ?>"<?php if($this->isOrderCurrent('short_description')): ?> selected="selected"<?php endif; ?>>
                 FRAGRANCE
            </option>
            <option value="<?php echo $this->getOrderUrl('price', 'asc') ?>"<?php if($this->isOrderCurrent('price') && $this->getCurrentDirection() == 'asc'): ?> selected="selected"<?php endif; ?>>
                 PRICE (Low to High)
            </option>
            <option value="<?php echo $this->getOrderUrl('price', 'desc') ?>"<?php if($this->isOrderCurrent('price') && $this->getCurrentDirection() == 'desc'): ?> selected="selected"<?php endif; ?>>
                 PRICE (High to Low)
            </option>

(我错误地使用了 short_description 属性而不是添加自定义香味属性)

现在,我需要的功能是,当使用其中的每一个时,例如按价格排序,我希望它按价格排序,它已经这样做了,然后再按另一个自定义属性“范围”排序。因此,如果它显示所有 3 英镑的产品,它会显示每个范围内的它们聚集在一起,而不是目前看起来随机的东西。

这是我上线的最后一个主要绊脚石,所以任何帮助都将不胜感激。

我已经使用toolbar.php中的以下行设法通过了一些道路

if ($this->getCurrentOrder()) {
      $this->_collection->setOrder(array($this->getCurrentOrder(), 'range'), $this->getCurrentDirection());
 }

唯一的问题是它似乎首先按范围排序,而不是按价格排序,即所有范围 1 按价格排序,然后范围 2 按价格排序。在哪里我需要它按价格排序,范围是价格的子排序

4

1 回答 1

4

当您调用setOrder()products 集合时,它会检查给定属性是否为price,如果是则使用addAttributeToSort。这是因为按价格排序需要一些比通用sortOrder方法更具体的代码。
但是当您传递一个数组时,测试失败并使用公共代码,因此您可以尝试将两个setOrder()调用分开:

if ($this->getCurrentOrder()) {
    $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
    $this->_collection->setOrder('range', $this->getCurrentDirection());
 }
于 2012-07-16T09:53:30.040 回答