Magento 订单状态更改事件

Magento order status change events(Magento 订单状态更改事件)
本文介绍了Magento 订单状态更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想通过 Web 服务更改远程清单,我知道通过 Event Observer Method 可以触发我的代码,但我不知道哪个事件对完成我的任务有用,例如 on_order_complete,是否有更新的列表事件或更多文档?

I want to change via web service a remote inventory, I know that via Event Observer Method can triger my code, but I don't know which event is useful to complete my task, like on_order_complete, is there an updated list of events or more documentation?

推荐答案

如果您想在订单状态更改为任何状态或状态时分派事件,则需要插入自己的事件侦听器.这并不像听起来那么难.

If you want to dispatch an event when the state of an order changes to any status or state, then you'll need to insert your own event listener. This isn't as difficult as it sounds.

只需像这样覆盖Mage_Sales_Model_Order中的_setStatus函数...

Simply override the _setStatus function in Mage_Sales_Model_Order like so...

/**
 * Order model
 *
 * @category    WMG
 * @package     WMG_Sales
 * @author      Lee Bolding <lee.bolding@wmg.com>
 *
 *  Supported events:
 *  sales_order_status_before
 *  sales_order_status_after
 *
 * NOTE: Unfortunately, we can't override setState() as the protected _setState()
 * function is used by the registerCancellation() and _checkState() functions
 *  
 */
class WMG_Sales_Model_Order extends Mage_Sales_Model_Order
{
    /**
     * Order state protected setter.
     * By default allows to set any state. Can also update status to default or specified value
     * Сomplete and closed states are encapsulated intentionally, see the _checkState()
     *
     * @param string $state
     * @param string|bool $status
     * @param string $comment
     * @param bool $isCustomerNotified
     * @param $shouldProtectState
     * @return Mage_Sales_Model_Order
     */
    protected function _setState($state, $status = false, $comment = '', $isCustomerNotified = null, $shouldProtectState = false)
    {
        // dispatch an event before we attempt to do anything
        Mage::dispatchEvent('sales_order_status_before', array('order' => $this, 'state' => $state, 'status' => $status, 'comment' => $comment, 'isCustomerNotified' => $isCustomerNotified, 'shouldProtectState' => $shouldProtectState));

        // attempt to set the specified state
        if ($shouldProtectState) {
            if ($this->isStateProtected($state)) {
                Mage::throwException(Mage::helper('sales')->__('The Order State "%s" must not be set manually.', $state));
            }
        }
        $this->setData('state', $state);

        // add status history
        if ($status) {
            if ($status === true) {
                $status = $this->getConfig()->getStateDefaultStatus($state);
            }
            $this->setStatus($status);
            $history = $this->addStatusHistoryComment($comment, false); // no sense to set $status again
            $history->setIsCustomerNotified($isCustomerNotified); // for backwards compatibility
        }

        // dispatch an event after status has changed
        Mage::dispatchEvent('sales_order_status_after', array('order' => $this, 'state' => $state, 'status' => $status, 'comment' => $comment, 'isCustomerNotified' => $isCustomerNotified, 'shouldProtectState' => $shouldProtectState));

        return $this;
    }
}

您现在可以为观察者订阅新创建的 sales_order_status_beforesales_order_status_after 事件

You can now subscribe observers to the newly created sales_order_status_before and sales_order_status_after events

这篇关于Magento 订单状态更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)