Magento订单状态迷思

01require_once('app/Mage.php');
02umask(0);
03Mage::app('default');
04 
05$order = Mage::getModel('sales/order');
06$order->loadByIncrementId(100000001); // 100000001为订单编号
07 
08// 获取订单状态
09$status = $order->getStatus();
10$state  = $order->getState();
11 
12echo $status;
13echo "\r\n";
14echo $state;
15 
16// 设置订单状态
17$order->setStatus(Mage_Sales_Model_Order::STATE_PROCESSING);
18$order->save();

订单有两个状态变量:state和status,这让人困惑,只有测试下了,于是下了个单,然后在Magneto后台处理订单,得出下面的状态值。

011. 新订单
02state  : new
03status : pending
04 
052. 配送后
06state  : processing
07status : processing
08 
093. 收款后
10state  : processing
11status : processing
12 
134. 订单完成
14state  : complete
15status : complete
16 
175. 订单取消
18state  : canceled
19status : canceled
20 
216. 订单关闭
22state  : closed
23status : closed
24 
257. 订单挂起
26state  : holded
27status : holded

在Magento代码文件app\code\core\Mage\Sales\Model\Order.php中定义了订单的状态常量:

01/**
02 * Order model
03 *
04 * Supported events:
05 *  sales_order_load_after
06 *  sales_order_save_before
07 *  sales_order_save_after
08 *  sales_order_delete_before
09 *  sales_order_delete_after
10 *
11 * @author Magento Core Team <core@magentocommerce.com>
12 */
13class Mage_Sales_Model_Orderextends Mage_Sales_Model_Abstract
14{
15 
16    /**
17     * Order states
18     */
19    constSTATE_NEW             = 'new';
20    constSTATE_PENDING_PAYMENT = 'pending_payment';
21    constSTATE_PROCESSING      = 'processing';
22    constSTATE_COMPLETE        = 'complete';
23    constSTATE_CLOSED          = 'closed';
24    constSTATE_CANCELED        = 'canceled';
25    constSTATE_HOLDED          = 'holded';
26    constSTATE_PAYMENT_REVIEW  = 'payment_review';// added magento 1.4
27 
28    /**
29     * Order flags
30     */
31    constACTION_FLAG_CANCEL    = 'cancel';
32    constACTION_FLAG_HOLD      = 'hold';
33    constACTION_FLAG_UNHOLD    = 'unhold';
34    constACTION_FLAG_EDIT      = 'edit';
35    constACTION_FLAG_CREDITMEMO= 'creditmemo';
36    constACTION_FLAG_INVOICE   = 'invoice';
37    constACTION_FLAG_REORDER   = 'reorder';
38    constACTION_FLAG_SHIP      = 'ship';
39    constACTION_FLAG_COMMENT   = 'comment';
40 
41    // ...
42}
43</core@magentocommerce.com>

其中,pending_payment, payment_review 没有测试到,后来发现是支付(Paypal, Amazon Pay)过程中引入的状态。具体请看Magento orders: states and statuses,文章阐述了Order States and Order Statuses的区别, 还有如何在Magento订单中增加自定义状态。