开发者

Magento - no event for newsletter subscribe & unsubscribe

开发者 https://www.devze.com 2023-03-02 04:47 出处:网络
Why are there no events dispatched on or around the newsletter subscription/un-subscription process either in the customer or newsletter modules.

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


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)


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;
}


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.


the newsletter modul hooks to event: customer_save_after

0

精彩评论

暂无评论...
验证码 换一张
取 消