Magento - no events to subscribe to newsletters and unsubscribe

Why there are no events sent during the subscription process / without subscribing to newsletters or as part of client modules or newsletters.

The only alternative that I have encountered at the moment is to use rewriting for the subscriber model so that it matches some code around.

Does anyone have a good alternative to this - or am I missing something

+6
source share
4 answers

Here is what just worked for me on 1.7.0.2. I know that this thread is outdated, but publish it here if it can help anyone (since there is not much information about this event):

* NOTE: Replace myco_myextension with your unique extension name: *

In the config.xml file:

<newsletter_subscriber_save_commit_after> <observers> <myco_myextension_model_observer> <class>Myco_Myextension_Model_Observer</class> <method>subscribedToNewsletter</method> </myco_myextension_model_observer> </observers> </newsletter_subscriber_save_commit_after> 

In Observer.php:

 public function subscribedToNewsletter(Varien_Event_Observer $observer) { $event = $observer->getEvent(); $subscriber = $event->getDataObject(); $data = $subscriber->getData(); $statusChange = $subscriber->getIsStatusChanged(); // Trigger if user is now subscribed and there has been a status change: if ($data['subscriber_status'] == "1" && $statusChange == true) { // Insert your code here } return $observer; } 
+11
source

I came across a situation where I needed to listen to events of subscription / unsubscription. I came across this question and thought that I would leave this solution for those who might need it:

By adding an observer to the newsletter_subscriber_save_before event in your config.xml:

 <global> .... <events> .... <newsletter_subscriber_save_before> <observers> <your_unique_event_name> <class>yourgroupname/observer</class> <method>newsletterSubscriberChange</method> </your_unique_event_name> </observers> </newsletter_subscriber_save_before> </events> </global> 

You can then call getSubscriber() (in the context of $observer->getEvent() , see the following code block) in your observer to get the Mage_Newsletter_Model_Subscriber model, which allows you to receive subscriber data. This works for subscription and unsubscribe cases.

 public function newsletterSubscriberChange(Varien_Event_Observer $observer) { $subscriber = $observer->getEvent()->getSubscriber(); //now do whatever you want to do with the $subscriber //for example if($subscriber->isSubscribed()) { //... } //or if($subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED) { //... } elseif($subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED) { //.. } } 

It so happened that it is really easy. These model events are super powerful and allow you to do things like it's super easy. Impossible to refuse free features!

For quick reference: what data is provided by the model Mage_Newsletter_Model_Subscriber (1.7)

+10
source

The newsletter/subscriber model is a regular Magento model from the look and feel, so it should still send some events from the above classes. Take a look at something like newsletter_subscriber_create_after and newsletter_subscriber_delete_after for some possible event hooks.

+6
source

newsletter module for the event: customer_save_after

0
source

Source: https://habr.com/ru/post/886976/


All Articles