This article is a solution If you want to add new custom sorter or filter options such as below:
New product,
Name, Instock,
Price Low to high,
Price High to Low.
Create a custom module and follow the below step.
1. Create file di.xml on app/code/Magemonkeys/Sorting/etc
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Catalog\Model\Config"> <plugin name="Magemonkeys_Sorting::addCustomSortOptions" type="Magemonkeys\Sorting\Plugin\Model\Config" /> </type> <type name="Magento\Catalog\Block\Product\ProductList\Toolbar"> <plugin name="Magemonkeys_Sorting::implementCustomSortOptions" type="Magemonkeys\Sorting\Plugin\Product\ProductList\Toolbar" /> </type> </config> |
2. Create file Config.php on app/code/Magemonkeys/Sorting/Plugin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php namespace Magemonkeys\Sorting\Plugin\Model; class Config { public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options) { //Remove default sorting options unset($options['position']); unset($options['name']); unset($options['price']); //New sorting options $options['newest'] = __('New product'); $options['is_salable'] = __('Stock'); $options['name'] = __('Name'); $options['price_desc'] = __('Price High to Low'); $options['price_asc'] = __('Price Low to High'); return $options; } } |
3. Create file Toolbar.php on app/code/Magemonkeys/Sorting/Product/ProductList
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
<?php namespace Magemonkeys\Sorting\Plugin\Product\ProductList; class Toolbar { public function aroundSetCollection( \Magento\Catalog\Block\Product\ProductList\Toolbar $toolbar, \Closure $proceed, $collection ) { $this->_collection = $collection; $currentOrder = $toolbar->getCurrentOrder(); $currentDirection = $toolbar->getCurrentDirection(); $result = $proceed($collection); if ($currentOrder) { switch ($currentOrder) { case 'newest': if ($currentDirection == 'desc') { $this->_collection->getSelect()->order('e.created_at ASC'); } elseif ($currentDirection == 'asc') { $this->_collection->getSelect()->order('e.created_at DESC'); } break; case 'is_salable': if ($currentDirection == 'desc') { $this->_collection->getSelect()->order('qty ASC'); } elseif ($currentDirection == 'asc') { $this->_collection->getSelect()->order('qty DESC'); } break; case 'name': if ($currentDirection == 'desc') { $this->_collection->getSelect()->order('name ASC'); } elseif ($currentDirection == 'asc') { $this->_collection->getSelect()->order('name DESC'); } break; case 'price_desc': if ($currentDirection == 'desc') { $this->_collection->getSelect()->order('price ASC'); } elseif ($currentDirection == 'asc') { $this->_collection->getSelect()->order('price DESC'); } break; case 'price_asc': if ($currentDirection == 'desc') { $this->_collection->getSelect()->order('price DESC'); } elseif ($currentDirection == 'asc') { $this->_collection->getSelect()->order('price ASC'); } break; default: $this->_collection ->setOrder($currentOrder, $currentDirection); break; } } //var_dump($currentDirection); return $result; } } |
[crayon-63e097e9d1c95254598585/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.