Reputation: 717
I've had a look through the few posts i've found related to sorting and not come up with the required solution.
I'm currently using the following code to sort products in the category view fields to override the default options and sort by attributes in 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>
(I mistakenly used the short_description attribute instead of adding a custom fragrance attribute)
now, the functionality I require is that when each one of these is used, for example sort by price, I would like it to sort by price which it already does but then to be secondarily sorted by another custom attribute 'range'. so that should it show all £3 product's it shows them from each range clumped together rather than, what seems randomly at the moment.
this is my last major stumbling block from going live, so any help would be greatly appreciated.
I've managed to make a little through roads using the following line in toolbar.php
if ($this->getCurrentOrder()) {
$this->_collection->setOrder(array($this->getCurrentOrder(), 'range'), $this->getCurrentDirection());
}
the only problem with that is that it seems to sort by range first rather than say price, i.e. all of range1 ordered by price then range 2 ordered by price. where as i need it to order by price with the range a sub-ordering of the prices
Upvotes: 3
Views: 1629
Reputation: 4214
When you call setOrder()
on a products collection, it checks if the given attribute is price
, and if so uses addAttributeToSort
instead. This is due to the fact that sorting on price needs some more specific code than what's done by the generic sortOrder
method.
But as you pass an array, the test fails and the common code is used, so you can try to separate the two setOrder()
calls instead :
if ($this->getCurrentOrder()) {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
$this->_collection->setOrder('range', $this->getCurrentDirection());
}
Upvotes: 4