观察者不保存订单

发布于 2024-11-01 03:19:27 字数 1274 浏览 4 评论 0原文

我构建了一个监听 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红颜悴 2024-11-08 03:19:27

您需要将属性添加到 quote sales 模型中 - 才能实现此目的。

由于 Magento 会将定义的

* 从 quote 复制到 order,因此您需要扩展 config.xml 也相应地:

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

*请参阅 Mages_Salesconfig.xml

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

云醉月微眠 2024-11-08 03:19:27

在此之前是否发生过交易?根据我的经验,在事务仍在进行时尝试保存模型是行不通的。我必须在交易后将观察者移至另一个事件,例如“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文