在Magento订单状态更改事件上触发观察者 [英] Trigger observer on Magento order status change events

查看:76
本文介绍了在Magento订单状态更改事件上触发观察者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在订单收到 processing 状态后自动在其上创建发票.在我的扩展程序中应该观察哪个事件来实现这一目标?

I need to automatically create an invoice on an order once it receives the processing status. Which event should be observed in my extension to achieve this?

我正在尝试,但是此代码无法正常工作,怎么回事?

I'm trying but this code is not working, what can it be?

.etc/modules中的.xml

.xml in etc/modules

<?xml version="1.0"?>
<config>
<modules>
    <Atwix_Orderhook>
        <active>true</active>
        <codePool>community</codePool>
    </Atwix_Orderhook>
</modules>
</config>

app/code/...中的config.xml ...

config.xml in app/code/...

<?xml version="1.0"?>
<config>
<modules>
    <Atwix_Orderhook>
        <version>1.0</version>
    </Atwix_Orderhook>
</modules>
<global>
    <models>            
        <orderhook>
            <class>Atwix_Orderhook_Model</class>
        </orderhook>
    </models>
    <events>
        <sales_order_save_after>
            <observers>
                <auto_invoice_order>
                    <type>singleton</type>
                    <class>Atwix_Orderhook_Model_Observer</class>
                    <method>implementOrderStatus</method>
                </auto_invoice_order>
            </observers>
        </sales_order_save_after>
    </events>

</global>
</config>

Oberver.php在应用程序/代码/...中

Oberver.php in app/code/...

<?php
class Atwix_Orderhook_Model_Observer 
{
public function implementOrderStatus($event)
{
    $order = $event->getOrder();

    if ($order == 'processing') {
        if ($order->canInvoice())
            $this->_processOrderStatus($order);
    }
    return $this;
}
                        private function _processOrderStatus($order)
                        {
                            $invoice = $order->prepareInvoice();

                            $invoice->register();
                            Mage::getModel('core/resource_transaction')
                               ->addObject($invoice)
                               ->addObject($invoice->getOrder())
                               ->save();

                            $invoice->sendEmail(false, '');
                            return true;
                        }
}

推荐答案

您可以观察 sales_order_save_after ,然后使用类似以下内容检查目标订单状态:

You can observe sales_order_save_after and then check for the order state you are targeting with something like this:

public function exampleEventHandler(Varien_Event_Observer $observer)
{
    /* @var Mage_Sales_Model_Order $order */
    $order = $observer->getOrder();
    $stateProcessing = $order::STATE_PROCESSING;
    // Only trigger when an order enters processing state.
    if ($order->getState() == $stateProcessing && $order->getOrigData('state') != $stateProcessing) {
        ...
    }
    ...
}

这篇关于在Magento订单状态更改事件上触发观察者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆