0

How to check if a plugin really work?

files are:

app/code/Vendor/Module/etc/di.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
    <type name="Magento\Store\Model\Store">
      <plugin name="set_default_currency" type="Vendor\Module\Plugin\Store\Model\Store" disabled="false" />
    </type>
</config>

app/code/Vendor/Module/Plugin/Store/Model/Store.php

<?php
namespace Vendor\Module\Plugin\Store\Model;

class Store
{
    
    private $_logger;

    public function __construct(
        \Psr\Log\LoggerInterface $logger
    )
    {
        $this->_logger = $logger;
    }
    
    public function afterGetDefaultCurrencyCode(
        \Magento\Store\Model\Store $subject,
        $result
    )
    {
        $this->_logger->debug("GEOIP::".$result);
     }
     ....

The plugin seems to never been called.

The method Magento\Store\Model\Store->getDefaultCurrencyCode() is one in the Limitation of plugins?

Limitations

Plugins can not be used on following:

  • Final methods
  • Final classes
  • Non-public methods
  • Class methods (such as static methods)
  • __construct
  • Virtual types
  • Objects that are instantiated before Magento\Framework\Interception is bootstrapped

EDIT

The reason was a silly typo mistake in registration.php file.

I want to add a note for those who find themselves in this situation.

My aim was to update the default currency based on the user's geo location.

Although it is a good method to create a plugin for the getDefaultCurrencyCode function, I ran into the issue of Full Page Cache, that literally drove me crazy.

Remembering the famous acronym "RTFM" I found an easy solution in: Configure page variations

Hope Will Help Someone...

5
  • Did you tried adding breakpoint and debugging with xdebug? Commented Aug 5, 2020 at 11:06
  • no, but i don get any error from magento log o server log
    – krybbio
    Commented Aug 5, 2020 at 11:18
  • There shouldn't be an error, I ment more like checking if that block is executed. Your code is fine check application deploy mode or if logging is enabled if you are in default or developer mode. Commented Aug 5, 2020 at 11:25
  • I'm in developer mode, logging is enabled end works everywhere else, the method \Magento\Store\Model\Store::getDefaultCurrencyCode() is called, if I hardcode the method in the core file with exit instruction it break the page.
    – krybbio
    Commented Aug 5, 2020 at 11:30
  • try changing $this->_logger->debug for $this->_logger->critical if you are not in developer mode the debug won't log anything
    – Rus0
    Commented Aug 6, 2020 at 7:27

0

Your Answer

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

Browse other questions tagged or ask your own question.