Home » Php » php – Observer doesn't save order

php – Observer doesn't save order

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’ve build an observer which listens on the sales_convert_quote_to_order event. The event is triggered and I just want to add a value to an attribute of the order. The attribute is set – as printed in the log – but magento doesn’t save the order. What I’m doing wrong?

Observer.php

public function addLangToOrder($observer){
        Mage::log('catching convert_quote_to_order_after');
        $order = $observer->getEvent()->getOrder();
        $order->setCustomerLanguage(Mage::app()->getStore()->getCode());
        $order->save();
        Mage::log($order->getCustomerLanguage());
    }

config.xml

<events>
    <sales_convert_quote_to_order>
        <observers>
            <accustomer>
                <type>singleton</type>
                <class>Ac_Customer_Model_Observer</class>
                <method>addLangToOrder</method>
            </accustomer>
        </observers>
    </sales_convert_quote_to_order>
</events>

I’ve added the attribute customer_language through an install script

$customer_lang = 'customer_language';
$installer->addAttribute('order', $customer_lang, array('type'=>'varchar'));

The customer_language column is present in my sales_flat_order table. But it doesn’t get saved.

I’m using Magento 1.4.1.1

How to&Answers:

You need to add your attribute to both – quote and sales model – to make this work.

As Magento will copy a defined <fieldset>* from quote to order, you need to extend the config.xml of your overriding class accordingly, too:

<config>
    <!-- : -->
    <global>
        <fieldsets>
            <sales_convert_quote>
                <customer_language><to_order>*</to_order></customer_language>
            </sales_convert_quote>
        </fieldsets>
    </global>
    <!-- : -->
</config>

*see the config.xml of Mages_Sales

Answer:

is there a transaction occurring before this? its been my experience that trying to save a model when a transaction is still in progress doesn’t work. I had to move the observer to another event after the transaction like “sales_model_service_quote_submit_after” instead.