11

I would like to add the checkbox newsletter in the billing form when the user choose to register directly in the checkout process.. How can I do that?

  • 2
    Do you happen to have any plans to utilize MailChimp for your newsletter mailing needs? The MageMonkey extension by ezbizmarts has a configuration option to add newsletter signup to the checkout process. – Darren Felton Jan 15 '15 at 19:05
15

hi you can add this by add below code at billing.phtml

 <input type="checkbox" name="is_subscribed" 
  title="<?php echo $this->__('Sign Up for Newsletter') ?>" value="1"  checked="checked" class="checkbox" />

using the event checkout_submit_all_after subscribe the customer to newsletter

  <global>
 <events>
    <checkout_submit_all_after> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_submit_all_after_handler> <!-- identifier of the event handler -->
            <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
            <class>magento52274/observer</class> <!-- observers class alias -->
            <method>AssignNewletter</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_submit_all_after_handler>
        </observers>
      </checkout_submit_all_after>
    </events>
  </global>

And Observer code is :

   public function AssignNewletter($observer) {
            $event = $observer->getEvent();
            $order = $event->getOrder();
        $Quote =$event->getQuote();
        if (in_array($Quote()->getCheckoutMethod(), array('register','customer'))):

        if (Mage::app()->getFrontController()->getRequest()->getParam('is_subscribed')){
        $status = Mage::getModel('newsletter/subscriber')->subscribe($Quote->getBillingAddress()->getCustomerEmail());
        }
        endif;
   }

Full Module:

Step1: create config.xml at app/code/local/Stackexchange/Magento52274/etc/ and code is

<?xml version="1.0"?>
<config>
  <modules>
    <Stackexchange_Magento52274>
      <version>1.0.0</version>
    </Stackexchange_Magento52274>
  </modules>
  <global>
    <models>
      <magento52274>
        <class>Stackexchange_Magento52274_Model</class>
      </magento52274>
    </models>
    <events>
      <checkout_submit_all_after> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_submit_all_after_handler> <!-- identifier of the event handler -->
            <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
            <class>magento52274/observer</class> <!-- observers class alias -->
            <method>AssignNewletter</method>  <!-- observer's method to be called -->
          </checkout_submit_all_after_handler>
        </observers>
      </checkout_submit_all_after>
    </events>
  </global>
</config> 

Step2: create Observer.php at app/code/local/Stackexchange/Magento52274/Model/ and code is

<?php
class Stackexchange_Magento52274_Model_Observer
{

    public function AssignNewletter(Varien_Event_Observer $observer)
    {
    $event = $observer->getEvent();
            $order = $event->getOrder();
        $Quote =$event->getQuote();
        if (in_array($Quote()->getCheckoutMethod(), array('register','customer'))):

            if Mage::app()->getFrontController()->getParam('is_subscribed', false)){
        $status = Mage::getModel('newsletter/subscriber')->subscribe($Quote->getBillingAddress()->getEmail());
         }
        endif;
    }

}

Step3: create Module app/etc/modules/Stackexchange_Magento52274.xml and code is

<?xml version="1.0"?>
<config>
  <modules>
    <Stackexchange_Magento52274>
      <active>true</active>
      <codePool>local</codePool>
      <version>1.0.0</version>
    </Stackexchange_Magento52274>
  </modules>
</config>

Step4: And also need add newsletter field at billing.phtml a

<input type="checkbox" name="is_subscribed" 
  title="<?php echo $this->__('Sign Up for Newsletter') ?>" value="1"  checked="checked" class="checkbox" />
  • 2
    This could probably use a little cleaning up - at the very least, some of the code under "Full Module" doesn't match the code earlier in the answer. – Mike Mar 9 '16 at 16:25
  • 2
    nice, just that ->getBillingAddress()->getEmail() does not always gets the email, especially when there is a paypal email involved. getCustomerEmail is safer. – Claudiu Creanga Mar 11 '16 at 13:28
  • Please disregard this answer. I have no idea how this could get this many upvotes. This will not work! You are not able to access billing form fields in checkout_submit_all_after observers. Please have a look at this implementation: magento.stackexchange.com/questions/219460/… – Michael Thessel Apr 30 at 21:26

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