When a customer comes to the store then sometimes they try to Sort By Price product in cheapest order or low to high order product or high to low order product.
So here we have developed a module that provides the facilities to sort the product based on the price in ascending order and descending order.
Magento 2 Sort By Price Filter Basic File
In order to create a SortByPrice module in ascending and descending order, we have to create four basic files.
app/code/Wishusucess/SortByPrice/registration.php
app/code/Wishusucess/SortByPrice/etc/module.xml
app/code/Wishusucess/SortByPrice/Plugin/Catalog/Block/Toolbar.php
app/code/Wishusucess/SortByPrice/Plugin/Catalog/Model/Config.php
Step 1:
Firstly we have to register our module through the registration.php file.
app/code/Wishusucess/SortByPrice/registration.php
<?php /** * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ /* |-------------------------------------------------------------------------- | Wishsucess SortByPrice Filter |-------------------------------------------------------------------------- | | SortByPrice High To Low and Low To High Filter | | @author Hemant Singh | http://www.wishusucess.com/ */ \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Wishusucess_SortByPrice', __DIR__ );
Step 2:
app/code/Wishusucess/SortByPrice/etc/module.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Wishusucess_SortByPrice" setup_version="1.0.0" /> </config>
Step 3: Sort By Price Low To High Filter Toolbar
app/code/Wishusucess/SortByPrice/Plugin/Catalog/Block/Toolbar.php
<?php /* |-------------------------------------------------------------------------- | Wishsucess SortByPrice Filter |-------------------------------------------------------------------------- | | SortByPrice High To Low and Low To High Filter | | @author Hemant Singh | http://www.wishusucess.com/ */ namespace Wishusucess\SortByPrice\Plugin\Catalog\Block; class Toolbar { public function aroundSetCollection(\Magento\Catalog\Block\Product\ProductList\Toolbar $subject, \Closure $proceed, $collection) { $currentOrder = $subject->getCurrentOrder(); $result = $proceed($collection); if($currentOrder) { if($currentOrder == 'high_to_low') { $subject->getCollection()->setOrder('price', 'desc'); } elseif ($currentOrder == 'low_to_high') { $subject->getCollection()->setOrder('price', 'asc'); } elseif ($currentOrder == 'mostviewed') { $subject->getCollection()->setOrder('mostviewed'); } } else { $subject->getCollection()->getSelect()->reset('order'); $subject->getCollection()->setOrder('price', 'asc'); } return $result; } }
Step 4:
app/code/Wishusucess/SortByPrice/Plugin/Catalog/Model/Config.php
<?php /* |-------------------------------------------------------------------------- | Wishsucess SortByPrice Filter |-------------------------------------------------------------------------- | | SortByPrice High To Low and Low To High Filter | | @author Hemant Singh | http://www.wishusucess.com/ */ namespace Wishusucess\SortByPrice\Plugin\Catalog\Model; class Config { public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $results) { $results['low_to_high'] = __('Price - Low To High'); $results['high_to_low'] = __('Price - High To Low'); $results['mostviewed'] = __('Recommended'); return $results; } }
Step 5: Create Attribute High To Low and Low To High and Most Viewed
Now you have to inside of the Magento 2 admin panel and choose Admin=>Stores=>Attribute=>Product=>Add Attribute
And create the following attributes:
- Low To High
- High To Low
- Most viewed
If you have multiple attributes set then you have to add the above attributes in that attributes set otherwise you have to add in the default attribute.
Now, Run Following Command:
php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento setup:static-content:deplpy -f php bin/magento cache:clean
Conclusion of Sort By Price Filter:
Now we have successfully been able to provide sort options through the above code for Low to High & High to Low and Most viewed product filter. But if you are still facing any kind of issue we are here to help you. Please post your comment here.
Download Link:
Wishusucess SortByPrice Filter Magento 2 Extension
Related Post:
Wishusucess AdminMenu: How to Create Magento 2 Admin Menu Module
Search AutoComplete: Magento 2 Module Add All Category for Search
Recommended Post:
Magento 2.4 Installation Guide: How to Install Magento 2.4.2
Magento Store: Best 36 Magento Websites Example in The World
SEO Packages: How Much Do SEO Packages Cost in India, SEO Pricing