1

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.

1 Answer 1

1

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.

3
  • I am selecting the orders with this $orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status', $oldstatus); This is selecting all the order with specific status. Your code will get the date for the order when received an status complete right ? Also if i use ->addAttributeToFilter('status', array('eq'=>$oldstatus)) does it will work ? Commented Oct 27, 2016 at 8:29
  • Whenever any order status is changed, if adds it into a history. Its kind of a log which will have date, order no, old status and new status etc. As you mentioned you need to get a date of when the order received a specific order status, this might be helpful for you. Commented Oct 27, 2016 at 9:37
  • Just as a quick addition, this only applies if the order stays is changed through the magento admin panel; if the status is changed by your custom extension for example it is not tracked
    – tim.baker
    Commented Sep 4, 2017 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.