2

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 ?

2 Answers 2

3

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!

3
  • It means, for any kind of text/label changes throughout the site, we just need to include our custom translation file under i18n ? For example if we need to change form field names only without extending even then we could use it ? Jan 19, 2018 at 10:27
  • Exactly, we can change text/label throughout the site using Magento translations. For more info: devdocs.magento.com/guides/v2.2/frontend-dev-guide/translations/… Jan 19, 2018 at 10:40
  • Its worked for me Thanks dear. I am working on magento2.3.1 Jul 1, 2019 at 11:23
0

I've tried different approach in Magento 2.3.5-p1. I've just used inline translation table in database.

Why? Inline translation take preference over translation files.

For example, to translate Position to New (Novedad in spanish), I executed this mysql query:

INSERT INTO translation (string, store_id, translate, locale, crc_string) VALUES 
('Position', '0', 'Novedad', 'es_ES', '1937236688'), 
('Position', '0', 'New', 'en_US', '1403912869')¡; 

Keep in mind that you should calculate decimal crc32 for the string on the crc_string column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.