Skip to content

Instantly share code, notes, and snippets.

@sukanta1983
Created March 21, 2015 05:29
Show Gist options
  • Save sukanta1983/1a296e31e42c0f88df93 to your computer and use it in GitHub Desktop.
Save sukanta1983/1a296e31e42c0f88df93 to your computer and use it in GitHub Desktop.
change order status in magento
<?php
require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
$from_date = $_GET['fdate'];
$today = date("Y-m-d");
try{
$orders = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('created_at', array('from' => $from_date, 'to' => $today))
->addAttributeToFilter('status', array('neq' => Mage_Sales_Model_Order::STATE_COMPLETE));
foreach($orders as $order){
/*========================================================*/
if($_GET['invoice'] == 'y'){
if(!$order->canInvoice()){
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));
continue;
}
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
if (!$invoice->getTotalQty()){
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
}
//$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
//Or you can use
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
$invoice->register();
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
}
/*========================================================*/
$order->setData('state', "complete");
//$order->setState(Mage_Sales_Model_Order::STATE_COMPLETE, true);
$order->setStatus("complete");
$history = $order->addStatusHistoryComment('Order was set to Complete by our automation tool.', false);
$history->setIsCustomerNotified(false);
$order->save();
echo "Order id - " .$order->getId() . 'has changed order status COMPLETED.';
}
}catch(Exception $e){
echo $e->getMessage();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment