Magento订单状态更改事件

时间:2010-06-15 15:18:04

标签: php magento

我想通过Web服务更改远程库存,我知道通过事件观察器方法可以解决我的代码,但我不知道哪个事件对完成我的任务很有用,比如on_order_complete,是否有更新的列表事件或更多文档?

4 个答案:

答案 0 :(得分:16)

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

只需覆盖 _setStatus 中的 Mage_Sales_Model_Order 功能,就像这样......

/**
 * 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事件订阅观察者

答案 1 :(得分:2)

我想更好的解决方案是注意更改,而不使用重写:

http://www.cartware.de/blog/detail/article/kein-magento-event-fuer-statusaenderung/

通过阅读代码,即使是用德语写的,也可以获得线索......

答案 2 :(得分:1)

几个星期前我做了blog post关于这个问题(其中包含了Magento CE 1.4的完整事件列表)。

您下订单时可能感兴趣的事件是sales_order_place_after,在下订单后会被调用(严重!)。

希望有所帮助!

谢谢, 乔

答案 3 :(得分:0)

使用grep查找事件列表,它必须类似于

grep -rn -A2 --include="*.php" dispatchEvent /var/www/magento/

或类似的......