8

Why are there no events dispatched on or around the newsletter subscription/un-subscription process either in the customer or newsletter modules.

The only alternative i am faced with at the moment is to use a rewrite for the subscriber model to fit some code in around here.

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

David
  • 81
  • 1
  • 2
  • Are you using the native Magento subscription functionality? If you want something a little more advanced - use the MailChimp plugin. It connects straight to the API and offers a lot more advanced functionality (including auto responders). – Avisra Apr 29 '11 at 17:48
  • If you need an event to be thrown when this happens, I would extend the class file into your local directory and add the event call. From there you can build out an extension that gets called when that event is triggered. – Josh Pennington May 01 '11 at 18:50

4 Answers4

11

I encountered a situation where I needed to listen for subscription/unsubscription events. I encountered this question and thought I would leave this solution here for anyone that may need it:

By adding an observer to the event newsletter_subscriber_save_before 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 next code block) in your observer to get the Mage_Newsletter_Model_Subscriber model that allows you get data about the subscriber. This works for occurrences of subscription and unsubscriptions.

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) {
        //..
    }

}

So it turns out this is really easy. These model events are super powerful and let you do things like this super easily. Can't turn down free functionality!

For quick reference, here is what data the Mage_Newsletter_Model_Subscriber model provides (1.7)

Josh Davenport-Smith
  • 5,456
  • 2
  • 29
  • 40
11

Here's what just worked for me on 1.7.0.2. I know this thread is old, but posting it here in case it can help anyone (since there's not a lot of info about this event out there):

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

In config.xml:

        <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;
}
Ira Herman
  • 2,796
  • 1
  • 23
  • 22
6

The newsletter/subscriber model is a normal Magento model from the looks of it, so it should still dispatch some events from the upstream classes. Take a look at something like newsletter_subscriber_create_after and newsletter_subscriber_delete_after for some possible event hooks to use.

Joe Mastey
  • 26,809
  • 13
  • 80
  • 104
  • When someone unsubscribe from your newsletter Magento doesn't erase him subscription, but flag the email address as unsubscribed, so you may want use `newsletter_subscriber_save_after` instead. – Anarcociclista May 16 '19 at 16:05
0

the newsletter modul hooks to event: customer_save_after

Kevin
  • 1,232
  • 10
  • 28