Forked from GideonBabu/Company\Module\Block\Product\ProductList\Toolbar.php
Created
December 19, 2019 13:51
-
-
Save mrmoyeez/e85661a00fffd0351b602dcf89e4a42e to your computer and use it in GitHub Desktop.
Magento 2: Product sorting high to low and low to high
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Company\Module\Block\Product\ProductList; | |
class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar | |
{ | |
public function setCollection($collection) { | |
$this->_collection = $collection; | |
$this->_collection->setCurPage($this->getCurrentPage()); | |
// we need to set pagination only if passed value integer and more that 0 | |
$limit = (int)$this->getLimit(); | |
if ($limit) { | |
$this->_collection->setPageSize($limit); | |
} | |
if ($this->getCurrentOrder()) { | |
switch ($this->getCurrentOrder()) { | |
case 'high_to_low': | |
$this->_collection->setOrder('price', 'desc'); | |
break; | |
case 'low_to_high': | |
$this->_collection->setOrder('price', 'asc'); | |
break; | |
default: | |
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection()); | |
break; | |
} | |
} | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment