Magento – Magento 2 main.CRITICAL: Plugin class doesn’t exist

magento2module

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

}

 

Best Answer

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.

Related Topic