0

I am writing my own admin email notifications module.

So far it has New Order and Low Stock observers set up and working just fine.

I want to add the Newsletter subscribe/unsubscribe observer to it and what I have works fine but when changing newsletter subscription status of a Customer (by saving the Customer in dashboard) it seems to send notification emails twice (first with current subscription status and then for new changed one ... for example if I change customer from Subscribed to Unsubscribed it will send email that there is new subscriber then email that subscriber has been unsubscribed

Observer

<?php
namespace Hapex\AdminEmailNotifications\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Hapex\AdminEmailNotifications\Helper\Data;
use Hapex\AdminEmailNotifications\Helper\Email;
use Magento\Framework\Message\ManagerInterface;

class NewsletterObserver implements ObserverInterface
{

    protected $date;

    protected $helperData;

    protected $helperEmail;

    protected $messageManager;


    public function __construct(
        DateTime $date,
        Data $helperData,
        Email $helperEmail,
        ManagerInterface $messageManager
    ) {
        $this->date = $date;
        $this->helperData = $helperData;
        $this->helperEmail = $helperEmail;
        $this->messageManager = $messageManager;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        try {
            $event = $observer->getEvent();
            $subscriber = $event->getSubscriber();
            $enable = $this->helperData->isEnabled();
            if ($enable)
            {
                $subscriberStatus = $subscriber->getSubscriberStatus();
                // subscriberStatus = 1 subscribe
                // subscriberStatus = 3 unsubscribed
                if ($subscriberStatus == '1'  && $this->helperData->isNewsletterSubscribeEnabled())
                {
                    $this->sendMailNewsletterSubscribe($subscriber);
                }
                else if ($subscriberStatus == '3' && $this->helperData->isNewsletterUnsubscribeEnabled())
                {
                    $this->sendMailNewsletterUnsubscribe($subscriber);
                }
            }
        }
        catch (\Exception $e) {
                $this->messageManager->addErrorMessage("Error : " . $e->getMessage());
        }
    }

    private function sendMailNewsletterSubscribe($subscriber)
    {
        try {
            $storeId = $subscriber->getStoreId();
            $email = $subscriber->getEmail();
            $receivers = $this->helperData->getNewsletterReceivers($storeId);
            $emailTemplate = $this->helperData->getNewsletterSubscribeEmailTemplate($storeId);
            $templateVar = ['varEmail' => 'Email: '.$email,];
            $this->helperEmail->sendEmail($receivers, $emailTemplate, $templateVar, $storeId);
        }
        catch (\Exception $e) {
                $this->messageManager->addErrorMessage("Error : " . $e->getMessage());
        }
    }

    private function sendMailNewsletterUnsubscribe($subscriber)
    {
        try {
            $storeId = $subscriber->getStoreId();
            $email = $subscriber->getEmail();
            $receivers = $this->helperData->getNewsletterReceivers($storeId);
            $emailTemplate = $this->helperData->getNewsletterUnsubscribeEmailTemplate($storeId);
            $templateVar = ['varEmail' => 'Email: '.$email,];
            $this->helperEmail->sendEmail($receivers, $emailTemplate, $templateVar, $storeId);
        }
        catch (\Exception $e) {
                $this->messageManager->addErrorMessage("Error : " . $e->getMessage());
        }
    }
}

Event

<event name="newsletter_subscriber_save_after">
        <observer name="hapex_newsletter_observer" instance="Hapex\AdminEmailNotifications\Observer\NewsletterObserver" />
    </event>

Is there a good way to maybe save the status before the event triggered then take it in the observer and if it has changed do its thing so it will fire only once ... any other way to make so it doesn't fire twice on Customer update?

Thanks

2
  • did you get the solution? – Deeban Babu Jan 27 '20 at 5:06
  • unfortunately, nope :( – Deggial Jan 28 '20 at 6:11
0

You can use plugin for this, like I have created a plugin where only when customer get unsubscribe from frontend then I have added some customization, for this I have added plugin for

Magento\Newsletter\Model\Subscriber

Class's unsubscribe() method with after plugin My code is, In di.xml

<type name="Magento\Newsletter\Model\Subscriber">
        <plugin disabled="false" name="Nerdsinn_Queue_Plugin_Magento_Newsletter_Model_Subscriber" sortOrder="10" type="Nerdsinn\Queue\Plugin\Magento\Newsletter\Model\Subscriber"/>
    </type>

and then in /Nerdsinn/Queue/Plugin/Magento/Newsletter/Model/Subscriber.php

namespace Nerdsinn\Queue\Plugin\Magento\Newsletter\Model;

/**
 * Class Subscriber
 *
 * @package Nerdsinn\Queue\Plugin\Magento\Newsletter\Model
 */
class Subscriber
{

    public function afterUnsubscribe(
        \Magento\Newsletter\Model\Subscriber $subject,
        $result
    ) {
        //Your plugin code
        return $result;
    }
}

Here on $result you can getData() to access available data

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.