Magento – How to get order status change date

order-status

Here is my code:

        $orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status', $oldstatus);

        $now = time(); // or your date as well

        if($orders){
            foreach ($orders as $order) {
                $orderId = $order->getIncrementId();
                $CreatedAt = strtotime($order->getCreatedAt());
                $email = $order->getCustomerEmail();
                $datediff = $now - $CreatedAt;
                $Diff = floor($datediff/(60*60*24));
                $Diff = $Diff - 1;
                if($Diff >= $time){

                   try{ 
                        $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
                        $state = 'processing';
                        $status = $newstatus;

                        //If you choose to set $isCustomerNotified = true; 
                        // then it would make the necessary notification to customer.
                        if($EnableAutomatedStatusEmail==1){
                            $isCustomerNotified = true;
                        } else {
                            $isCustomerNotified = false;
                        }
                        /*
                        $order->setState($state, $status, $comment, $isCustomerNotified);
                        $order->save();

                        //Send email to customer. If not required then comment this line out.
                        if($EnableAutomatedStatusEmail==1){
                          $order->sendOrderUpdateEmail(true, $comment);
                        }
                        */
                    } 
                   catch(Exception $e) 
                   { 
                   Mage::log($e->getMessage()); 
                   } 


                    Mage::log("The order $orderId with status $oldstatus received new status $newstatus for customer with email $email!", null, "AutomatedStatus.log", true);          
                }

            }   
        }

With this code i am parsing all the orders searching for orders with specific order status. Then if a certain time passed from the creation of the order and the order is still in the specific order status the script is changing the order status.

Everything works fine.

The only problem is that i do not need to get the order created date getCreatedAt() but i need to get the date of when the order received a specific order status. For instance i need to know when the order received order status processing not when the order was created. Also i need to get this date in the same format i am receiving it from getCreatedAt().

Is it possible ?

I hope i have explained everything well, please if you have any questions do not hesitate to ask me in the comments.

Best Answer

Magento keeps a track of order status changes in a collection called "Order status history collection" You can use this to get what you need.

$collection = Mage::getResourceModel('sales/order_status_history_collection')
    ->addAttributeToSelect('created_at')
    ->addAttributeToFilter('status', array('eq'=>'complete'))
    ->load();

Or to be more specific you can get also add the order id or increament id to filter this collection further down.

Related Topic