skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. It Work’s for Magento 2.4 versions
    I am Assuming you have created a custom module Vendor/module_sort

    1. create di.xml file in vendor/module_sort/etc/

     <?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="MagentoCatalogModelConfig">
                    <plugin name="Vendor_module_sort::addCustomOptions" type="Vendormodule_sortPluginModelConfig" />
                </type>
                <preference for="MagentoCatalogBlockProductProductListToolbar" type="Vendormodule_sortPluginCatalogBlockToolbar" />
        </config>
    
    1. Create Config.php in Vendor/module_sort/plugin/Model

      <?php
        namespace Vendormodule_sortPluginModel;
        
        class Config  {
        
            public function afterGetAttributeUsedForSortByArray(MagentoCatalogModelConfig $catalogConfig, $options)
            {
               
                unset($options['name']);
                unset($options['price']);
        
                //New sorting options
                $customOption['high_to_low'] = __( 'Price High to Low' );
                $customOption['low_to_high'] = __( 'Price Low to High' );
                $customOption['name_Z_to_A'] = __( 'Product Title:Z-A' );
                $customOption['name_A_to_Z'] = __( 'Product Title:A-Z' );
        
                // $customOption['name'] = $default_options['name'];
        
                //Merge default sorting options with custom options
                $options = array_merge($customOption, $options);
        
                return $options;
            }
        }
    
    1. Create Toolbar.php in Vendor/module_sort/plugin/Catalog/Block/

     <?php
        namespace Vendormodule_sortPluginCatalogBlock;
        
        class Toolbar extends MagentoCatalogBlockProductProductListToolbar
        {
        
            /**
             * Set collection to pager
             *
             * @param MagentoFrameworkDataCollection $collection
             * @return MagentoCatalogBlockProductProductListToolbar
             */
            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()) {
                    if (($this->getCurrentOrder()) == 'position') {
                        $this->_collection->addAttributeToSort(
                            $this->getCurrentOrder(),
                            $this->getCurrentDirection()
                        );
                    } else {
                        if ($this->getCurrentOrder() == 'high_to_low') {
                            $this->_collection->setOrder('price', 'desc');
                        } elseif ($this->getCurrentOrder() == 'low_to_high') {
                            $this->_collection->setOrder('price', 'asc');
                        }elseif ($this->getCurrentOrder() == 'name_Z_to_A') {
                            $this->_collection->setOrder('name', 'desc');
                        }elseif ($this->getCurrentOrder() == 'name_A_to_Z') {
                            $this->_collection->setOrder('name', 'asc');
                        }
                    }
                }
                return $this;
            }
        
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search