1

I've searched and searched how to add the 'sign up for newsletter' checkbox (same as on the customer/account/create/ page), to the /checkout/onepage page, but can't find any definitive answer.

I'd like it at the 'billing information' stage if possible.

2

This is possible with a small extension ... here the most important steps:

  • create your own extension
  • app/code/community/[Vendor]/[Module]/etc/config.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <[Vendor]_[Module]>
                <version>1.0.0</version>
            </[Vendor]_[Module]>
        </modules>
        <global>
            <helpers>
                <newslettersubscribe>
                    <class>[Vendor]_[Module]_Helper</class>
                </newslettersubscribe>
            </helpers>
            <models>
                <newslettersubscribe>
                    <class>[Vendor]_[Module]_Model</class>
                </newslettersubscribe>
            </models>
        </global>
        <frontend>
            <events>
                <checkout_type_onepage_save_order_after>
                    <observers>
                        <newslettersubscribe>
                            <type>model</type>
                            <class>newslettersubscribe/observer</class>
                            <method>subscribeEventOnepageSaveOrderAfter</method>
                        </newslettersubscribe>
                    </observers>
                </checkout_type_onepage_save_order_after>
                <controller_action_postdispatch_checkout_onepage_saveBilling>
                    <observers>
                        <newslettersubscribe>
                            <type>model</type>
                            <class>newslettersubscribe/observer</class>
                            <method>subscribeEventSaveBilling</method>
                        </newslettersubscribe>
                    </observers>
                </controller_action_postdispatch_checkout_onepage_saveBilling>
            </events>
            <layout>
                <updates>
                    <newslettersubscribe>
                        <file>[Vendor]/[Module].xml</file>
                    </newslettersubscribe>
                </updates>
            </layout>
        </frontend>
    </config>
    
  • app/code/community/[Vendor]/[Module]/etc/Model/Observer.php

    class [Vendor]_[Module]_Model_Observer
    {
        public function subscribeEventSaveBilling(Varien_Event_Observer $observer)
        {
            $billing = $observer->getEvent()
                ->getControllerAction()
                ->getRequest()
                ->getParam('billing', array());
    
            $willSubscribe = isset($billing['subscribe']);
            Mage::getSingleton('checkout/session')->setWillSubscribe($willSubscribe);
    
            return $this;
        }
    
        public function subscribeEventOnepageSaveOrderAfter(Varien_Event_Observer $observer)
        {
            $willSubscribe = Mage::getSingleton('checkout/session')->getWillSubscribe();
    
            if ((bool)$willSubscribe) {
                $email = $observer->getEvent()->getOrder()->getData('customer_email');
                Mage::getModel('newsletter/subscriber')->subscribe($email, true);
            }
            return $this;
        }
    }
    
  • app/code/community/[Vendor]/[Module]/etc/Helper/Data.php

    class [Vendor]_[Module]_Helper_Data extends Mage_Core_Helper_Abstract
    {
        /** @var string $_moduleName Module name */
        protected $_moduleName = '[Vendor]_[Module]';
    
        public function isSubscribed()
        {
            $customer = Mage::getSingleton('customer/session')->getCustomer();
            if ($customer) {
                $customerEmail = $customer->getEmail();
                $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($customerEmail);
                if ($subscriber) {
                    return $subscriber->isSubscribed();
                }
            }
        }
    }
    
  • app/design/frontend/base/default/layout/[Vendor]/[Module].xml

    <layout version="0.1.0">
        <checkout_onepage_index>
            <reference name="checkout.onepage.billing">
                <block type="core/template" name="form.additional.info">
                    <action method="setTemplate">
                        <template>[Vendor]/[Module]/subscribe.phtml</template>
                    </action>
                </block>
            </reference>
        </checkout_onepage_index>
    </layout>
    
  • app/design/frontend/base/default/template/[Vendor]/[Module]/subscribe.phtml

    <?php
    $helper = Mage::helper('newslettersubscribe');
    ?>
    
    <li class="control">
        <input type="checkbox" name="billing[subscribe]" id="billing:subscribe" value="1" <?php if ($helper->isSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox" />
        <label for="billing:subscribe"><?php echo $helper->getFrontendLabel() ?></label>
    </li>
    

If I havn't missed anything this should a checkbox to your checkout billing section. If you want the checkbox at review step, read here Newsletter subscription at the checkout review.

| improve this answer | |
  • Thank you sv3n - I will be working on this tomorrow and will report back. – Andy Mar 22 '18 at 16:02
  • Hi sv3n With little knowledge of how to create extensions, I think I need step by step instructions. I'm confused at step 1 :-) I did manage to create the file and folder structure for the module and get it to display in the Admin/Advanced/Advanced Disable Modules Output list, but as to where the code above goes, I'm lost. – Andy Mar 26 '18 at 12:04
  • @Andy updated answer. Hope this helps a bit. – sv3n Mar 27 '18 at 22:13
  • Hi sv3n - again thank you! I created my extension (AJH_NewsSub) and can see it active in Admin. I replaced all [Vendor]/[Module] with AJH/NewsSub logic and created the following: app/code/community/AJH/NewsSub/etc/config.xml app/code/community/AJH/NewsSub/etc/Model/Observer.php app/code/community/AJH/NewsSub/etc/Helper/Data.php app/design/frontend/base/default/layout/AJH/NewsSub.xml app/design/frontend/base/default/template/AJH/NewsSub/subscribe.phtml This didn't display the subscribe checkbox. I am using a custom theme and will give detail in my next reply. – Andy Mar 28 '18 at 11:57
  • My custom theme uses the following to display the billing section of my checkout: frontend\argento\default\template\persistent/checkout/onepage/billing.phtml I've tried several places for the NewsSub.xml and subscribe.phtml files but no luck so far. This is helping me to understand the structure of Magento better and i'd love to get this working if possible. – Andy Mar 28 '18 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.