Magento – Change ‘Sort by Position’ text to ‘Sort by New’ Magento 2.2.6

i18nmagento2product-sortingtoolbar

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?

Best Answer

  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;
}
}

?>
Related Topic