I have a problem with a multistore site created with Magento 2.4. Right now the products from the category page and the search page are arranged by name. The options for sorting are: Alphabetical A – Z and Z – A, and by price from low to high and high to low. When I try to sort the products alphabetical everything works fine, but when I try by price it’s like a random sort. I also put a custom module but the same result. Instead, if I sort them by product id or by weight it is ok.
if ($currentOrder) {
if ($currentOrder == 'price_asc') {
$subject->getCollection()->setOrder('price', 'asc');
} elseif ($currentOrder == 'price_desc') {
$subject->getCollection()->setOrder('price', 'desc');
} elseif ($currentOrder == 'name_asc') {
$subject->getCollection()->setOrder('name', 'asc');
} elseif ($currentOrder == 'name_desc') {
$subject->getCollection()->setOrder('name', 'desc');
}
}
I also put ->getStoreId(1)
after getCollection()
but I have same the result. Edit: from what I saw instead of sort by price it’s a sort by product id
2
Answers
Steps to Add Magento 2 Sort by Price for Low to High & High to Low Options:
Step 1: Create registration.php file in appcodeVendorExtension
Step 2: Create module.xml file in appcodeVendorExtensionetc
Step 3: Create di.xml file in appcodeVendorExtensionetc
Step 4: Create Toolbar.php file in appcodeVendorExtensionPluginCatalogBlock
Step 5: Create Config.php file in appcodeVendorExtensionPluginCatalogModel
You can refer to the details here.
It Work’s for Magento 2.4 versions
I am Assuming you have created a custom module Vendor/module_sort