Home » Magento

Magento: How to change order status programmatically?

14 July 2010 13,136 views Popularity: 27% Share/Bookmark

email

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.

Note: This code is written and tested in Magento version 1.3.2.4. It might not work in later versions of Magento.

Related posts:

  1. Magento: Quick way to create order invoice programmatically
  2. Magento: How to change or reorder top links?
  3. Magento: Displaying / Adding Gift Message in Order
  4. Magento: Reindex Data Programmatically
  5. Magento: Set Random Order in Collection using RAND()
  6. Magento: How to change Currency symbol by Model Override ?
  7. Magento: How to change default page layout?
  8. Magento: Product Edit Warning: Invalid argument supplied for foreach()
  9. Magento: How to change Admin URL Path?
  10. Magento: Get order option of configurable and bundle product in cart
  • http://topsy.com/blog.chapagain.com.np/magento-how-to-change-order-status-programmatically/?utm_source=pingback&utm_campaign=L2 Tweets that mention Magento: How to change order status programmatically? | Mukesh Chapagain’s Blog — Topsy.com

    [...] This post was mentioned on Twitter by Joonkiri J, Tech Blog. Tech Blog said: Magento: How to change order status programmatically? (http://bit.ly/bi3AIT) #Magento #order_status [...]

  • http://www.magento-help.co.uk Chris Belfield

    Hey!

    I receive the error:

    Uncaught exception ‘Mage_Core_Exception’ with message ‘The Order State “closed” must not be set manually.’

    Can you help?

    Chris.

  • zimba

    i have the same problem

    Uncaught exception ‘Mage_Core_Exception’ with message ‘The Order State “closed” must not be set manually.’

    Can you help?

    zimba

  • Maks

    Hi men !

    If you have “Closed” and not “closed” it’s works !

    => Try this : “ucfirst(Mage_Sales_Model_Order::STATE_CLOSED)”

  • Shaniabudi

    This method doesn’t seem to work…
    I’ve tried :
    $order->setState(Mage_Sales_Model_Order::STATE_COMPLETE, true)->save();

    and got :
    Uncaught exception ‘Mage_Core_Exception’ with message ‘The Order State “complete” must not be set manually.’

  • Adevillaine

    This is because STATE_CLOSED and STATE_COMPLETED cannot be set with setState.

    Mage_Sales_Model_Order explicitly set an exception when you try to set them with the method just mention.

    I also need to set the state but to completed. The ucfirst is not really a good solution because what you are actually doing is to change the constant value, and breaks some validations Magento do.

    This is for Magento >= 1.4.0.1

  • Femiji

    the
    $order->setState(Mage_Sales_Model_Order::STATE_COMPLETE, true)->save();
    does not work in magento 1.4.2

    throws ‘The Order State “complete” must not be set manually.’

  • Josie

    This program is for the birds

  • http://www.fijnmedia.nl Vincent

    But this wil work

    $this->_setState(self::STATE_COMPLETE, true, ”, $userNotification);

  • Vishal

    How to set the product status as ‘DISABLED’ programatically????

  • deter

    Hi Mukesh, thanks for your posts about magento programming (they helped me very much). But in this case, when I call $order->save() method, it throws an error:

    Fatal error: Mage_Core_Model_Session_Abstract::addMessage(): The script
    tried to execute a method or access a property of an incomplete object.
    Please ensure that the class definition
    “Mage_Core_Model_Message_Collection” of the object you are trying to
    operate on was loaded _before_ unserialize() gets called or provide a
    __autoload() function to load the class definition in /myweb/app/code/core/Mage/Core/Model/Session/Abstract.php
    on line 238

    When not calling save() method, this error doesnt show, but order status isn´t saved :(
    Any solution for this?

    //sorry for my English

  • deter

    I reply for myself – there was session_start() used twice at start of the script. Sorry for mess, you can delete my posts.

  • deter

    I reply for myself – there was session_start() used twice at start of the script. Sorry for mess, you can delete my posts.

  • Sanket Shah

    This thing is not working. can you suggest some alternate method?

  • Sanket Shah

    This thing is not working. Can you please suggest some alternate method?

  • Ramesh589

    not working , can u say in detail

  • Hone

    $order->addStatusToHistory(Mage_Sales_Model_Order::STATE_COMPLETE);

  • http://www.prabowomurti.com sangprabo

    Try also, 

    $order->setData(‘state’, Mage_Sales_Model_Order::STATE_COMPLETE);
    $order->save();

  • Sam

    which php file do i put this code in?
    magento 1.5
    i am trying to change status of orders which have particular shipping method
    for eg.
    $order = Mage::getModel(‘sales/order’)->getCollection()->addFieldToFilter(‘shipping_description’,'some_random_shipping_name’))->getFirstItem();

    then I want to set the status which I have created using the new feature in 1.5 and assign that status to these orders.
     Are there any ideas how can I do this?
    Thanks

  • Bassam

    Can we do the same thing (set order state to “complete” or “processing”) using SOAP instead of API?