Magento: How to change order status programmatically

You often need to change order status programmatically when working with a project which requires to transfer order between Magento site and a third party system. Usually, Magento sends order info to processing system, this system sends back result of that order: ship, reject... And we need to change order status programmatically here.

First, you will need load your order. Here are 2 ways to load order with order id and order increment id.

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

Now we will see how to change order status. Basic method for doing this will be:

$order->setState(YOUR_NEW_ORDER_STATUS, true)->save();

And you can see a list of order status here:

  • Pending: Mage_Sales_Model_Order::STATE_NEW
  • Pending Payment: Mage_Sales_Model_Order::STATE_PENDING_PAYMENT
  • Processing: Mage_Sales_Model_Order::STATE_PROCESSING, true
  • Complete: Mage_Sales_Model_Order::STATE_COMPLETE
  • Closed: Mage_Sales_Model_Order::STATE_CLOSED
  • Canceled: Mage_Sales_Model_Order::STATE_CANCELED, true

So, to change an order to complete, you will write this code:

$order->setState(Mage_Sales_Model_Order::STATE_COMPLETE, true)->save();

3 thoughts on “Magento: How to change order status programmatically”

3 Responses to Magento: How to change order status programmatically

Comments