0

I am trying to send email when customer subscribes/unsubscribes from newsletter, however, my observer is triggering three times for some odd reason. Any ideas why or how to prevent it?

The following code logs "Test" three times in the log files...

Observer:

<?php
namespace VENDOR\MODULE\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface as Logger;

class NotifyAdmin implements ObserverInterface
{
    public function __construct(
        Logger $logger
    ) {
        $this->_logger = $logger;
    }

    public function execute(EventObserver $observer)
    {
        $this->_logger->info('Test');
    }
}
1
  • what event you are using? share the complete code. – Dhiren Vasoya Jul 21 at 10:42
1

There is a way to prevent it from having the observer react more than once, but it requires using the Registry to log the first call and skip any proceeding calls.

Your Answer

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

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