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

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. If you want to suggest any changes feel to leave your comment here or simply fork this code from its GitHub repository page.

You made it all the way down here so you must have enjoyed this post! You may also like:

Disabling Magento’s default newsletter transactional emails Vedran Sola
Vedran Sola, | 5

Disabling Magento’s default newsletter transactional emails

Custom Magento Events: Customer First Order Branko Ajzele
Branko Ajzele, | 7

Custom Magento Events: Customer First Order

Extending Order object and hooking on event in Magento Branko Ajzele
Branko Ajzele, | 25

Extending Order object and hooking on event in Magento

14 comments

  1. Magento provides checkboxes for subscribing in the checkout and the sign in pages. Set the checkboxes to checked and display none. simple

  2. Even when the order is successful, the subscriber is added as a ‘guest’ with no first and last name or other mapping fields. Can that be changed?

  3. Hi, How do I make subscriptions mandatory when customers/orders are added on the backend. This only seems to capture front end customer/orders additions. Thanks!!

  4. Thanks for the work,
    I would like to modify the code like this:
    1. I need to use it only during the checkuot
    2. I need to render the checkbox in the billing page so if someone want can to uncheck

    Someone can help me? I sow many plugins, but i have a custom class for onepage and some of them override it

    Thanks a lot

  5. @sulo:

    Line 48 creates block that renders first and last name input fields:

    app/design/frontend/base/default/template/inchoo/autosubscribe/customer/form/register.phtml

    <li class="fields">
        <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getFormData())->setForceUseCustomerAttributes(true)->toHtml() ?>
    </li>

    This block uses template from:
    app/design/frontend/base/default/template/customer/widget/name.phtml

    There are many reasons why this block isn’t created on you Magento register.phtml but I would start by examining template paths and var/log/exception.log for some useful info. Or you could just continue using your workaround 😉

  6. 					<label for="firstname" class="required"><em>*</em>Firstname</label>
    					<div class="input-box">
    					<input type="text" id="firstname" name="firstname" value="" title="firstname" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('firstname') ?>"  />
    					</div>
    					<label for="lastname" class="required"><em>*</em>Lastname</label>
    					<div class="input-box">
    					<input type="text" id="lastname" name="lastname" value="" title="lastname" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname') ?>"  />
    					</div>
  7. Great idea but when I uploade this the first name and last name were not visible and I could not register, received error that those info is required. I’m sure that there is a better solution, but with my knowledge I solved it like this:

    at /template/inchoo/autosubscribe/customer/form/register.phtml I added the code beneath in line 47

    					<label for="firstname" class="required"><em>*</em>Firstname</label>
    					<div class="input-box">
    					<input type="text" id="firstname" name="firstname" value="" title="firstname" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('firstname') ?>"  />
    					</div>
    					<label for="lastname" class="required"><em>*</em>Lastname</label>
    					<div class="input-box">
    					<input type="text" id="lastname" name="lastname" value="" title="lastname" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname') ?>"  />
    					</div>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.