观察者不保存订单
我构建了一个监听 sales_convert_quote_to_order
事件的观察者。该事件被触发,我只想向订单的属性添加一个值。该属性已设置 - 正如日志中打印的那样 - 但 magento 不会保存订单。我做错了什么?
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>
我已通过安装脚本添加了属性 customer_language
。
$customer_lang = 'customer_language';
$installer->addAttribute('order', $customer_lang, array('type'=>'varchar'));
customer_language
列出现在我的 sales_flat_order 表中。但它没有被保存。
我正在使用 Magento 1.4.1.1
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将属性添加到
quote
和sales
模型中 - 才能实现此目的。由于 Magento 会将定义的
*请参阅
Mages_Sales
的config.xml
You need to add your attribute to both -
quote
andsales
model - to make this work.As Magento will copy a defined
<fieldset>
* fromquote
toorder
, you need to extend theconfig.xml
of your overriding class accordingly, too:*see the
config.xml
ofMages_Sales
在此之前是否发生过交易?根据我的经验,在事务仍在进行时尝试保存模型是行不通的。我必须在交易后将观察者移至另一个事件,例如“sales_model_service_quote_submit_after”。
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.