Is there an event in Magento 2 for Newsletter Subscription from the Profile page?
How do I find out when a user updates their newsletter subscription preference from this page?
You can use the predispatch/postdispatch events triggerred by the controller action class. In your case:
controller_action_predispatch_newsletter_manage_save
to observe a behavior BEFORE the save method is called.controller_action_postdispatch_newsletter_manage_save
to observer a behavior AFTER the save method is called.$observer->getEvent()->getRequest()->getParam('is_subscribed');
Commented
Apr 14, 2017 at 8:56
Fernando Miguel's answer doesn't work, change
newsletter_subscriber_save_before
For
newsletter_subscriber_save_after
After that, you'll be able to get the subscriber ID. Or even get the email using
$observer->getEvent()->getSubscriber()->getSubscriberEmail()
All these functions are available too:
int getStoreId()
$this setStoreId(int $value)
string getChangeStatusAt()
$this setChangeStatusAt(string $value)
int getCustomerId()
$this setCustomerId(int $value)
string getSubscriberEmail()
$this setSubscriberEmail(string $value)
int getSubscriberStatus()
$this setSubscriberStatus(int $value)
string getSubscriberConfirmCode()
$this setSubscriberConfirmCode(string $value)
int getSubscriberId()
Subscriber setSubscriberId(int $value)
Beyond controller_action_predispatch
and controller_action_postdispatch
if you want to work with model events just declare on your Vendor/Module/etc/frontend/events.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="newsletter_subscriber_save_before">
<observer name="vendor_module_observer_myobserver" instance="Vendor\Module\Observer\MyObserver" />
</event>
</config>
On Magento\Newsletter\Model\Subscriber example you have $_eventPrefix
variable. This variable will be used on Magento\Framework\Model\AbstractModel to generate dynamic dispatches for models at before and after events. Also on Magento\Newsletter\Model\Subscriber example we have $_eventObject
to access the class methods as the following snippet observer example:
public function execute(Observer $observer)
{
$subscriberId = $observer->getEvent()->getSubscriber()->getId();
$this->logger->notice('subscriber ID: ' . $subscriberId);
}
Note that getSubscriber()
uses get + $_eventObject = 'subscriber'
value as described on var comments.