3

Expected functionality: user checks the "Subscribe" checkbox in the OnePage Checkout's review step and is subscribed to the newsletter.

So far I have managed to target the right event (after submitting checkout form):

config.xml

<events>        
    <checkout_submit_all_after>
        <observers>
            <checkout_newsletter>
                <class>News_Custom_Model_Observer</class>
                <method>assignNewsletter</method>
            </checkout_newsletter>
        </observers>
    </checkout_submit_all_after>   
</events>

info.phtml contains

<div class="text-left mt10 mb30">
     <div class="input-checkbox">
        <input id="subscribe_newsletter" type="checkbox" name="is_subscribed" value="1" checked="checked">
        <label for="is_subscribed">Subscribe for newsletter</label>
    </div>
</div>

Observer.php

public function assignNewsletter(Varien_Event_Observer $observer) 
{
    $event = $observer->getEvent();
    $order = $event->getOrder();
    $quote = $event->getQuote();

    if ($observer->getRequest()->getParam('is_subscribed', false)) {
        Mage::log('got here');
        Mage::getModel('newsletter/subscriber')->subscribe($quote->getCustomerEmail());
    } else {
        Mage::log($observer->getRequest()->getParams());
    }
} 

What happends is that $observer->getRequest()->getParams() only contains shipping method. So the issue is to pass/capture the data from checkout view to observer.

Any help or guidance is much appreciated.

0
  • First, make sure that you template code iss inside a <form> tag.

    <form action="" id="checkout-review" onsubmit="return false;">
        ...
        <div class="text-left mt10 mb30">
            <div class="input-checkbox">
                <input id="subscribe_newsletter" type="checkbox" name="is_subscribed" value="1" checked="checked">
                <label for="is_subscribed">Subscribe for newsletter</label>
            </div>
        </div>
        ...
    </form>
    
  • add first observer to controller_action_predispatch_checkout_onepage_saveOrder to store user input in checkout session

    public function checkoutSaveOrder(Varien_Event_Observer $observer)
    {
        $subscribe = $observer->getEvent()
            ->getControllerAction()
            ->getRequest()
            ->getParam('is_subscribed', 0);
    
        Mage::getSingleton('checkout/session')->setIsSubscribed($subscribe);
    
        return $this;
    }
    
  • add second observer to checkout_type_onepage_save_order_after to subscribe user after order is placed

    public function checkoutSaveOrderAfter(Varien_Event_Observer $observer)
    {
        $subscribe = Mage::getSingleton('checkout/session')->getIsSubscribed();
    
        if ((bool)$subscribe) {
            $email = $observer->getOrder()->getCustomerEmail();
            Mage::getModel('newsletter/subscriber')->subscribe($email);
        }
        return $this;
    }
    
  • which one is the better answer, I see you edited both. Appreciate it. Currently having trouble where the input field is not passed, does the form need to be around the final review submit button too? – snh_nl Mar 8 '18 at 19:57
  • @snh_nl Wich one? What do you mean? The form has just to be arrond your input field. Test with RWD theme ... maybe you have a different behavoir? – sv3n Mar 12 '18 at 12:21
  • Sorry misread your question as an answer ;) got it working – snh_nl Mar 12 '18 at 12:22
  • @snh_nl if it was useful to you, please leave some feedback :) – sv3n Mar 22 '18 at 20:08

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.