如何在Magento中自动创建运输 [英] How to create shipping automatically in Magento

查看:62
本文介绍了如何在Magento中自动创建运输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改Magento的默认工作流程.因此,我应该在客户购买商品后立即自动创建运输.(当客户看到收据"页面时).我不知道我应该从哪里开始.我开始谷歌搜索一些扩展,但现在没有运气.这就是为什么我来这里.有谁知道我可以从哪里开始解决这个问题?谢谢!

I need to change Magento default workflow. So, I should automatically create shipping as soon as customers buy something.(when customers see Receipt page). I am not sure where should I start. I started googling for some extension, but no luck for now. That's why I came here. Does anyone have an idea where can I start resolving this problem? Thanks!

推荐答案

请勿将此类代码放入视图文件中.除了@ user2729065提到的一般做法外,它还很糟糕:如果客户在付款后没有返回到谢谢"页面,则该代码将不会运行. 更好的方法是使用观察者创建自定义模块.为此,将以下代码添加到您的模块etc/config.xml文件中:

You should never put this kind of code in view files. Besides it being bad practice in general, as @user2729065 mentioned: if the customer does not return to the thank you page after the payment, the code will not be run. Better is to create a custom module with an observer. To do this, add the following code to your module etc/config.xml file:

<global>
    <events>
        <sales_order_invoice_pay>
            <observers>
                <[my]_[module]_automatically_complete_order>
                    <class>[module]/observer</class>
                    <method>automaticallyShipCompleteOrder</method>
                </[my]_[module]_automatically_complete_order>
            </observers>
        </sales_order_invoice_pay>
    </events>
</global>

将my_module更改为您的模块名称.支付发票时,这会触发.

Change my_module to your module name. This will triger when an invoice is paid.

然后在My/Module/Model/Observer.php中创建观察者

Then create the Observer in My/Module/Model/Observer.php

<?php

class My_Module_Model_Observer
{
/**
 * Mage::dispatchEvent($this->_eventPrefix.'_save_after', $this->_getEventData());
 * protected $_eventPrefix = 'sales_order';
 * protected $_eventObject = 'order';
 * event: sales_order_invoice_pay
 */
public function automaticallyShipCompleteOrder($observer)
{
    $order = $observer->getEvent()->getInvoice()->getOrder();

    if ($order->getState() == Mage_Sales_Model_Order::STATE_PROCESSING) {

        try {
            $shipment = $order->prepareShipment();
            $shipment->register();

            $order->setIsInProcess(true);
            $order->addStatusHistoryComment('Shippment automatically created.', false);

             Mage::getModel('core/resource_transaction')
                ->addObject($shipment)
                ->addObject($shipment->getOrder())
                ->save();
        } catch (Exception $e) {
            $order->addStatusHistoryComment('Could not automaticly create shipment. Exception message: '.$e->getMessage(), false);
            $order->save();
        }
    }

    return $this;
}

}

这将检查订单是否仍在处理中(付款后的状态.).如果是这样,请尝试创建货件.创建发货后,订单将自动将其状态更改为已完成.

This will check if the order is still in Processing ( the state it gets after it is paid for.). And if so, try to create the shipment. After the shipment is created, the order will automatically change its state to completed.

如果您希望在下订单后立即创建货件(在创建发票并付款之前),请更改

If you want the shipment to get created directly after the order is placed (before the invoice is created and paid for.), change

<sales_order_invoice_pay> 

<sales_order_place_after> 

config.xml中.并且由于此观察者返回的是订单而不是发票,因此也需要更改:

in config.xml. And because this observer returns an order and not an invoice, also change:

$order = $observer->getEvent()->getInvoice()->getOrder();

$order = $observer->getEvent()->getOrder();

代码基于 Inchoo ,所以大部分功劳归功于他们.

Code is based on an example from Inchoo so most credits go to them.

这篇关于如何在Magento中自动创建运输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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