Magento – Run a method in Observer after Billing Information

billingcheckoutquote

I have this in config.xml

<global>
    <events>
        <sales_quote_save_after>
            <observers>
                <save_after>
                    <type>singleton</type>
                    <class>Practice_Payment_Model_Observer</class>
                    <method>saveQuoteAfter</method>
                </save_after>
            </observers>
        </sales_quote_save_after>
        .
        .

And my Observer.php

public function saveQuoteAfter($evt){
    $quote = $evt->getQuote();

    // $post variable is checkbox in the Billing Information
    $post = Mage::app()->getFrontController()->getRequest()->getPost();

    if(isset($post['custom']['consultancyservice'])){
        $var = $post['custom']['consultancyservice'];
        $quote->setConsultancyservice($var);
    } else {
        $var = 'false';
        $quote->setConsultancyservice($var);
    }
    $model = Mage::getModel('custom/custom_quote');
    $model->deteleByQuote($quote->getId(),'consultancyservice');
    $model->setQuoteId($quote->getId());
    $model->setKey('consultancyservice');
    $model->setValue($var);
    $model->save();
}

$post is variable is checkbox in the Billing Information

This updates the value in the database. But whenever I go beyond 'Billing Information' it resets. Maybe because sales_quote_save_after runs at all steps in the checkout?

I have also tried controller_action_predispatch_checkout_onepage_savebilling and controller_action_postdispatch_checkout_onepage_savebilling but they are not working.

Thanks to anyone who can help!!

Best Answer

Hi instead of using sales_quote_save_after use

sales_quote_address_save_after or sales_quote_address_save_before

and youobserver code is wroong

public function saveQuoteAfter($observer){
    /* change here */
$addressObject=$observer->getEvent()->getQuoteAddress();


    $quote = $observer->getEvent()->getQuoteAddress()->getQuote();

    // $post variable is checkbox in the Billing Information
    $post = Mage::app()->getFrontController()->getRequest()->getPost();

    if(isset($post['custom']['consultancyservice'])){
        $var = $post['custom']['consultancyservice'];
        $quote->setConsultancyservice($var);
    } else {
        $var = 'false';
        $quote->setConsultancyservice($var);
    }
    $model = Mage::getModel('custom/custom_quote');
    $model->deteleByQuote($quote->getId(),'consultancyservice');
    $model->setQuoteId($quote->getId());
    $model->setKey('consultancyservice');
    $model->setValue($var);
    $model->save();
}
Related Topic