2

This must/should be easily achieved...I am simply hoping to change the 'Sort By Position' text to 'Sort by New' in the Magento category list toolbar.

I have attempted to do this by creating the folder i18n, within my theme folder in the app/ and pub/ directories, and adding the translation in 'en_US.csv'.

This doesn't seem to have had any affect... can anyone please suggest how this is achieved?

4 Answers 4

3
  1. di.xml override Magento\Catalog\Model\Config.php

app\code\[Vendor]\[Module]\etc\frontend\di.xml

    <?xml version="1.0"?>

        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
            <preference for="Magento\Catalog\Model\Config" type="[Vendor]\[Module]\Model\Config" />
        </config>
  1. Create Config.php file

app\code\[Vendor]\[Module]\Model\Config.php

<?php
namespace [Vendor]\[Module]\Model;

class Config extends \Magento\Catalog\Model\Config
{

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

    return $options;
}
}

?>
1
  • Thanks bro Sweety Masmiya !
    – River
    Jan 8, 2021 at 7:15
1

Well, since they're both English, I don't think that approach will work.

You need to make some changes to the source code.

In your module, in the file module_dir/Model/Config.php:

<?php
namespace Vendor\Module\Model;

class Config extends \Magento\Catalog\Model\Config
{
    /**
     * Retrieve Attributes Used for Sort by as array
     * key = code, value = name
     *
     * @return array
     */
    public function getAttributeUsedForSortByArray()
    {
        $options = ['position' => __('New')];
        foreach ($this->getAttributesUsedForSortBy() as $attribute) {
            /* @var $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */
            $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
        }

        return $options;
    }
}

And in your di.xml add the following line:

<preference for="Magento\Catalog\Model\Config" type="Vendor\Module\Model\Config" />
2
  • Any registration file needed Jorge1o1 or is this all I need? Nov 29, 2018 at 9:15
  • This should be fine as long as you already have a module. If you don't already have a module then you do need to create a module with these files
    – Jorge1o1
    Nov 29, 2018 at 18:06
1

Create plugin

<type name="Magento\Catalog\Model\Config">
        <plugin name="custom_sortby_text" type="Vendor\Module\Plugin\Catalog\Model\Config" />
</type>

config.php

namespace Vendor\Module\Plugin\Catalog\Model;

class Config
{
    public function afterGetAttributeUsedForSortByArray(
    \Magento\Catalog\Model\Config $catalogConfig,
    $options
    ) {
        $options['position'] = __('New');
        return $options;
    }

}
0

I'm not sure if by 2.4.6 this has become easier, or if this has always been this easy...

Now, you can simply open (or create):

<mage_root>/app/design/frontend/<Theme_Vendor>/<Theme_Child>/i18n/en_GB.csv

(or whatever language you're using)

and add:

Position,Whatever

So in the case of this question, it would be:

Position,New

In my case it is now:

Position,Recommended

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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