RSS

Update order status programmatically in magento

10 Jul

There are 2 ways to do the same.

  1. We can update order status of product in magento by using this below code. setState() function will do this for us, all we need is just pass the required arguments like state to be updated, status of the state, comment for the status change.
    Double click below to select all codes
    1
    2
    3
    4
    5
    6
    7
    8
    $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
    $state = 'new';
    $status = 'label_generated_by_user';
    $comment = 'You have generated you shipping label';
    $isCustomerNotified = true;
    $order->setState($state, $status, $comment, $isCustomerNotified);
    $order->save();
    $order->sendOrderUpdateEmail(true, $comment);//Sending email notification to customer.

    After updating order we can send notification to customer by using sendOrderUpdateEmail().

     

  2. 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();
    }

Advertisements
 
Leave a comment

Posted by on July 10, 2013 in Magento

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: