Magento: How to change order status programmatically?

piaoling  2011-06-20 14:50:55

Here, I will show you, how you can change your order status programmatically (with PHP coding).

First, you need to load your order.

If you have order id, you can load order in the following way:-

 

$orderId = YOUR_ORDER_ID;
$order = Mage::getModel('sales/order')
                ->load($orderId);

If you have order increment id, you can load order in the following way:-

$orderIncrementId = YOUR_ORDER_INCREMENT_ID;
$order = Mage::getModel('sales/order')
                ->loadByIncrementId($orderIncrementId);

Now, here is the code to change order status:-

/**
 * change order status to 'Completed'
 */
$order->setState(Mage_Sales_Model_Order::STATE_COMPLETE, true)->save();

Similarly, you can change the order status to pending, processing, canceled, closed, holded, etc.

/**
 * change order status to 'Pending'
 */
$order->setState(Mage_Sales_Model_Order::STATE_NEW, true)->save();
 
/**
 * change order status to 'Pending Paypal'
 */
$order->setState(Mage_Sales_Model_Order::STATE_PENDING_PAYMENT, true)->save();
 
/**
 * change order status to 'Processing'
 */
$order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();
 
/**
 * change order status to 'Completed'
 */
$order->setState(Mage_Sales_Model_Order::STATE_COMPLETE, true)->save();
 
/**
 * change order status to 'Closed'
 */
$order->setState(Mage_Sales_Model_Order::STATE_CLOSED, true)->save();
 
/**
 * change order status to 'Canceled'
 */
$order->setState(Mage_Sales_Model_Order::STATE_CANCELED, true)->save();
 
/**
 * change order status to 'Holded'
 */
$order->setState(Mage_Sales_Model_Order::STATE_HOLDED, true)->save();

You can also cancel an order in the following way:-

if($order->canCancel()) {
    $order->cancel()->save();
}

Hold an order:-

if($order->canHold()) {
    $order->hold()->save();
}

Unhold an order:-

if($order->canUnhold()) {
    $order->unhold()->save();
}

Hope this helps. Thanks.

from :https://aniscartujo.com/webproxy/default.aspx?prx=http://blog.chapagain.com.np/magento-how-to-change-order-status-programmatically/

通过翻墙获得

类别 :  magento(258)  |  浏览(3649)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: