{ Hello Magento 2 }

解决 Magento 2 应用问题,更注重深度挖掘。(ง •̀_•́)ง

0%

Magento 2 Newsletter Subscription Event

vendor/module/etc/events.xml

1
2
3
4
5
6
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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