Magento 2.2.2 – Rename Sort By Options Label

magento2sorting

On product listing page, I need to rename the label of Sort by Option Position to Most Popular.

Below are the two files responsible for sorting

\vendor\magento\module-catalog\Block\Product\ProductList\Toolbar.php
\vendor\magento\module-catalog\Model\Config.php

In Toolbar.php you can see

$this->_availableOrder = $this->_catalogConfig->getAttributeUsedForSortByArray();

which in turn calls getAttributeUsedForSortByArray() from Config.php that returns array of available attributes to sort listing collection.

In Config.php it has below function where Position label is given

public function getAttributeUsedForSortByArray()
    {
        $options = ['position' => __('Position')];
        foreach ($this->getAttributesUsedForSortBy() as $attribute) {
            /* @var $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */
            $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
        }

        return $options;
    }
  1. To change the label do I need to extend Model/Config.php in my
    custom theme and change $options = ['position' => __('Position')]; to $options = ['position' => __('My Custom label')]; ?
  2. Can this be done by extending sorter.phtml ?
  3. Can we change sort by options label from admin ?

Best Answer

You can change sort by option label using Magento Translations directly through comma-separated value (.csv) file without making any changes in code.

For example, your store locale en_US then just add:

"Position","Most Popular"

in app/design/frontend/VendorName/theme/i18n/en_US.csv

Here, replace VendorName and theme with your current theme directory name.

Note: if your store running on production mode then you may need to deploy static content in order see changes on front-end.

Happy coding!

Related Topic