【问题标题】:Observer doesn't save order观察者不保存订单
【发布时间】:2011-08-04 11:25:41
【问题描述】:

我已经构建了一个监听器来监听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

【问题讨论】:

  • 你的事件被触发了吗? //哎呀抱歉,我没看清楚……
  • 您说的是哪个版本的 Magento?你能指定..!
  • 抱歉,Subesh 忘记了版本。请检查我的更新
  • 您的日志文件中没有任何内容吗?而且,您不能使用现有的store_id 订单属性而不是创建一个新属性吗?对我来说,它看起来像是重复的。
  • 我的日志中没有任何内容。我不知道你在哪里看到重复的。我不存储 store_id - 我想将 store->code 属性存储到我的订单...

标签: php magento observer-pattern


【解决方案1】:

您需要将您的属性添加到 quote sales 模型中 - 才能使这项工作。

由于 Magento 会将定义的 &lt;fieldset&gt;* 从 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

【讨论】:

    【解决方案2】:

    在此之前是否有交易发生?根据我的经验,当交易仍在进行时尝试保存模型不起作用。在“sales_model_service_quote_submit_after”等事务之后,我不得不将观察者转移到另一个事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-14
      • 2021-04-13
      相关资源
      最近更新 更多