0

Function formatTxt() inside class Magento\Directory\Model\Currency is in charge of formatting the 'Price' throughout the shop.

I want to change its return value, but I don't know how.
Thanks for your helps in advance :)

--------- EDIT thanks to @Ankit Shah -----------

As mentioned in "How to Override Core Block, Model and controller in Magento2" I created a module named Amaj_CorePlugin and did a module:enable and setup:upgrade to activate it.

Its app/code/Amaj/CorePlugin/etc/di.xml code is as follows:

<?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\Directory\Model\Currency" type="Amaj\CorePlugin\Model\Currency" />
</config>

The app/code/Amaj/CorePlugin/Model/Currency.php code is as follows:

<?php

namespace Amaj\CorePlugin\Model;

class Currency extends \Magento\Directory\Model\Currency
{

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Locale\FormatInterface $localeFormat,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Directory\Helper\Data $directoryHelper,
        \Magento\Directory\Model\Currency\FilterFactory $currencyFilterFactory,
        \Magento\Framework\Locale\CurrencyInterface $localeCurrency,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        array $data = []
    )
    {
        parent::__construct(
            $context, $registry, $localeFormat, $storeManager, $directoryHelper, $currencyFilterFactory, $localeCurrency,
            $resource, $resourceCollection, $data
        );

        $this->logger = $logger;
    }

    /**
     * @param float $price
     * @param array $options
     * @return string
     */
    public function formatTxt($price, $options = [])
    {
        $this->logger->info('++++++++++++++++Amaj formatTxt');
        return 'customOutput$';
    }
}

My formatTxt doesn't get called. I think the problem is that the Magento_Directory:Model\Currency has not been set inside any di.xml at all. So the "preference" is useless against it.
What do you think should I do?

  • Override that function in your custom module magento.stackexchange.com/questions/86497/… – Ankit Shah Sep 21 '16 at 9:20
  • @Ankit Shah: Thanks for your help. I edited the post following the link you gave, but it still doesn't work. – Pro-Magento Sep 21 '16 at 10:30
  • app/core/ it should be app/code right in Magento 2 – Ankit Shah Sep 21 '16 at 10:34
  • It was a misspelling in the post (which now I have fixed it) but I had it right in the file system. I think the problem is that the Magento_Directory:Model\Currency has not been set inside any di.xml at all. So the "preference" is useless against it. Am I right? – Pro-Magento Sep 21 '16 at 10:54
  • @Ankit Shah: I deleted the module from the db manually and recompiled the code. Guess what? The 'preference' solution worked! If you make an answer I'll accept it. Thanks :) – Pro-Magento Sep 22 '16 at 8:01

Your Answer

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

Browse other questions tagged or ask your own question.