3

Hi guys,

I'm pretty new to Magento, so excuse me if I may be asking something obvious.

 

I'm trying to override function formatTxt() of class Magento\Directory\Model\Currency

 

I did a little search and I don't know if I've got the best way for it, so please guide me if I've chosen a wrong path:

I think doing it through plugins is the right way.

 

The problem is that when I render a search-result page, no result is being rendered. When I looked inside 'var/log/system.log', the following error is being logged:

[2016-09-17 16:47:49] main.CRITICAL: Plugin class Amaj\CorePlugin\Model\Currency\Plugin doesn't exist [] []

 

Please help me to find the problem.

Thank you for your helps in advance :)

 

 

-------------------------------------- The files ---------------------------------------------------------

I created a module with the following structure:

 

 

I have defined the files like the following:

 

registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Amaj_CorePlugin',
    __DIR__
);

module.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Amaj_CorePlugin" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Directory"/>
        </sequence>
    </module>
</config>

InstallSchema.php

<?php

namespace Amaj\CorePlugin\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $setup->endSetup();
    }
}

 

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">
    <type name="Magento\Directory\Model\Currency">
        <plugin name="amaj-core-plugin-currency" type="Amaj\CorePlugin\Model\Currency\Plugin" sortOrder="10" disabled="false" />
    </type>
    <!--preference for="Magento\Directory\Model\Currency" type="Amaj\CorePlugin\Model\CurrencyPlugin" /-->
</config>

Plugin.php

<?php

namespace Amaj\CorePlugin\Model\Currency;

class Plugin
{
    protected $logger;

    public function __construct(\Psr\Log\LoggerInterface $logger) {
        $this->logger = $logger;
        parent::__construct();
    }

    /**
     * @param float $price
     * @param array $options
     * @return string
     */
    public function aroundFormatTxt(
        $subject,
        \Closure $proceed,
        $price,
        $options = []
    )
    {
        $logger->info('Amaj aroundFormatTxt');
        return '2';
    }

}

 

4
  • Did you run php bin/magento setup:upgrade, if yes check your module name(Amaj) in the list Sep 18, 2016 at 17:01
  • Tried to delete var/generation and flush your Magento Cache? Sep 19, 2016 at 2:53
  • @krishna ijjada at i95Dev: Thank you for your help. Yes I have ran setup:upgrade and now the module name appears in admin panel. And also the di.xml is being processed because it realizes that should pick my class as plugin. But it gives an error finding the class. Sep 20, 2016 at 6:31
  • @Khoa TruongDinh: Thank you for your help. I deleted var/generation, but now flushing cache produces an error: 'There are no commands defined in the 'cache namespace' and reloading the pages also fails producing a lot of php errors. Sep 20, 2016 at 6:40

2 Answers 2

3

I couldn't reproduce your error, but I got an other one.

Then I did some minor adjustments to your code and it worked.
First drop the InstallSchema.php file. It does nothing so it is useless.

Then make sure that your plugin file is in Amaj/CorePlugin/Model/Currency/Plugin.php and it looks like this:

<?php

namespace Amaj\CorePlugin\Model\Currency;

class Plugin
{
    protected $logger;

    public function __construct(\Psr\Log\LoggerInterface $logger) {
        $this->logger = $logger;
    }

    /**
     * @param $subject
     * @param \Closure $proceed
     * @param $price
     * @param array $options
     * @return string
     */
    public function aroundFormatTxt(
        $subject,
        \Closure $proceed,
        $price,
        $options = []
    )
    {
        $this->logger->info('Amaj aroundFormatTxt');
        return '2';
    }

}

you don't need parent::__construct(); in the constructor because there is no parent class.
And I replaced $logger->info with $this->logger->info because there is no $logger variable.

1
  • 1
    Thanks for the help. I think the main problem was the name of the module's folder. I named it 'module-core-plugin'. But after reading your post I renamed it to 'CorePlugin'. Now it's working. Have I got right? Sep 21, 2016 at 9:37
0

For the purpose of example, i have added below code and it is working fine. Check this approach may be this can be helpful.

Your model(result.php) is not in correct place.

\magento\vendor\magento\module-contact\etc\frontend\di.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Url\SecurityInfo">
        <arguments>
            <argument name="secureUrlList" xsi:type="array">
                <item name="contact" xsi:type="string">/contact/</item>
            </argument>
        </arguments>
    </type>
    <type name="Magento\Directory\Model\Currency">
        <plugin name="amaj-core-plugin-currency" type="Magento\Contact\Model\Plugin" sortOrder="10" disabled="false" />
    </type>
</config>

\magento\vendor\magento\module-contact\Model\Plugin.php

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Contact\Model;

class Plugin
{
    /**
     * @param float $price
     * @param array $options
     * @return string
     */
    public function aroundFormatTxt()
    {
        echo "fffffffffffff";
        exit;
    }
}

Note : Not recommended to edit core module, i showed this for just test purpose.

0

Your Answer

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

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