Magento – Set Order Status Programmatically

While working on a project this evening, I was tasked with setting the order status of all orders within September to Complete automatically, no notifications to the customer, and a comment was needed for exporting later.

Below is how it was accomplished. This was done on Magento Enterprise 1.12

File: /root/orderUpdates.php

require_once ("app/Mage.php");
ini_set("error_reporting",E_ALL);
ini_set("display_errors",true);
umask(0);
Mage::app('admin');

/*
*
* Brad Taylor @ www.texo.me - November 2012
* Grab all orders from month of September that are not Order Status = Complete, and set them to Complete.
*
*/
try
{
	$orders = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('created_at', array('from' => '2012-09-01', 'to' => '2012-09-30'))
	->addAttributeToFilter('status', array('neq' => Mage_Sales_Model_Order::STATE_COMPLETE));
	foreach($orders as $order)
	{
		//grab orders from above (all that are not complete from the month of September), and set them to Complete status programmatically.
		$order->setData('state', "complete");
		$order->setStatus("complete");
		$history = $order->addStatusHistoryComment('Order was set to Complete by our automation tool.', false);
		$history->setIsCustomerNotified(false);
		$order->save();
	}
}
catch(Exception $e)
{
	echo $e->getMessage();
}

That’s it! Pretty simple call, I also used my Magento – Get Orders within Date Range post for this!

Posted in Magento, Magento Enterprise, PHP