1

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?

2
  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

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" />
  • Any registration file needed Jorge1o1 or is this all I need? – YorkieMagento Nov 29 '18 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 '18 at 18:06

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.