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 ?
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!

| improve this answer | |
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.

| improve this answer | |

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.