Newsletter auto-subscribe on create account and place order in Magento


By default Magento provides a Newsletter feature, which enables store administrators to send newsletters to customers who have registered to receive them. Since most customers tend not to opt-in to any email subscription related services, you might need to automatically subscribe customers when they register or place an order. In this article I’ll present code and simple Magento extension to make it easier for you to accomplish this task.

Since complete Magento extension named Inchoo_AutoSubscribe is available from its GitHub repository page, here I’ll just include a few important code snippets. You can easily download code by clicking at ZIP icon inside GitHub repository page interface or using this link.
General idea is to create observers to capture sales_order_place_after and customer_register_success events, and there we can automatically subscribe customer to newsletter. One thing to note is that customers are left with option to manually unsubscribe using My Account -> Newsletter Subscriptions screen, and if they do unsubscribe, they wont be automatically subscribed next time they place an order.
First things first, the snippet from our config.xml file to register event observers:
app/code/community/Inchoo/AutoSubscribe/etc/config.xml
<config>
    <frontend>
        <events>
            <sales_order_place_after>
                <observers>
                    <inchoo_autosubscribe_sales_order_place_after>
                        <class>Inchoo_AutoSubscribe_Model_Observer</class>
                        <method>salesOrderPlaceAfter</method>
                    </inchoo_autosubscribe_sales_order_place_after>
                </observers>
            </sales_order_place_after>
            <customer_register_success>
                <observers>
                    <inchoo_autosubscribe_customer_register_success>
                        <class>Inchoo_AutoSubscribe_Model_Observer</class>
                        <method>customerRegisterSuccess</method>
                    </inchoo_autosubscribe_customer_register_success>
                </observers>
            </customer_register_success>
        </events>
    </frontend>
</config>
To hide Sign Up for Newsletter checkbox from Create an Account screen we must point customer_form_register block template to our own .phtml file using something like this inside our layout xml file:
app/design/frontend/base/default/layout/inchoo_autosubscribe.xml
<layout version="0.1.0">
    <customer_account_create>
        <reference name="customer_form_register">
            <action method="setTemplate">
                <template>inchoo/autosubscribe/customer/form/register.phtml</template>
            </action>
        </reference>
    </customer_account_create>
</layout>
Last but not least snippet is our observers code:
app/code/community/Inchoo/AutoSubscribe/Model/Observer.php
class Inchoo_AutoSubscribe_Model_Observer extends Varien_Object
{
    public function salesOrderPlaceAfter($observer)
    {
        $email = $observer->getEvent()->getOrder()->getCustomerEmail();
        Mage::log('salesOrderPlaceAfter: '.$email);
        $this->_autoSubscribe($email);
    }
    public function customerRegisterSuccess($observer)
    {
        $email = $observer->getEvent()->getCustomer()->getEmail();
        Mage::log('customerRegisterSuccess: '.$email);
        $this->_autoSubscribe($email);
    }
    protected function _autoSubscribe($email)
    {
        Mage::log('_autoSubscribe: '.$email);
        $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
        if($subscriber->getStatus() != Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED &&
                $subscriber->getStatus() != Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED) {
            $subscriber->setImportMode(true)->subscribe($email);
        }
    }
}
I sincerely hope you’ll find this code useful.

Reference :  http://inchoo.net 

No comments:

Post a Comment