Magento 2 Newsletter Subscription Event

vendor/module/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="newsletter_subscriber_save_after">
        <observer name="vendor_module_observer_myobserver" instance="Vendor\Module\Observer\MyObserver" />
    </event>
</config>

Vendor\Module\Observer\MyObserver.php

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;

class MyObserver implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(Observer $observer)
    {
        $subscriber = $observer->getEvent()->getSubscriber();
        $email = $subscriber->getEmail();
        $subscriberStatus = $subscriber->getSubscriberStatus();
        // subscriberStatus = 1 subscribe
        // subscriberStatus = 3 unsubscribed
        if ( $subscriberStatus == '1') {
            // your code
        }
    }
}

It works both on frontend and backend

参考链接

https://magento.stackexchange.com/questions/170157/magento-2-newsletter-subscription-event?noredirect=1&lq=1&utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

发表评论

电子邮件地址不会被公开。 必填项已用*标注