Magento – Change order status programmatically not working

edit-ordermagento-1order-statusorderssales-order

I have two environments, production and development. And I tried to run this code on root of Magento:

<?php

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

$order = Mage::getModel("sales/order")->loadByIncrementId(100000672);

$order->setData("state", "pending_recharge");
$order->setStatus("pending_recharge");
$history = $order->addStatusHistoryComment('A ser executada a rotina de recarga.', false);
$history->setIsCustomerNotified(false);
$order->save();

I am just changing the Increment Id from one environment to another. The acctually status and states of order is "complete".

On development the code is working, but on production the status of order does't save.

I don't know how debug to find this error, I believe it to be in the database, maybe some foreign key.

Another thing I noticed that I have two payment methods and this only happening with one.

And I checked who pending_recharge status exist in both environments.

Best Answer

Change order status programmatically

//Status Updated start here
require_once 'app/Mage.php';

// Retrieve order
$_order = new Mage_Sales_Model_Order();
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$_order->loadByIncrementId($orderId);

umask(0);
Mage::app('default');
/*
    ##Magento Status List
    const STATE_NEW             = 'new';
    const STATE_PENDING_PAYMENT = 'pending_payment';
    const STATE_PROCESSING      = 'processing';
    const STATE_COMPLETE        = 'complete';
    const STATE_CLOSED          = 'closed';
    const STATE_CANCELED        = 'canceled';
    const STATE_HOLDED          = 'holded';
    const STATE_PAYMENT_REVIEW  = 'payment_review';
 */

$order = Mage::getModel('sales/order')->loadByIncrementID($orderId);
$order->setState(Mage_Sales_Model_Order::STATE_PENDING_PAYMENT, true);
$order->save();

You may change your code as below:

$order = Mage::getModel('sales/order')->loadByIncrementID(100000672);
$order->setState(Mage_Sales_Model_Order::STATE_PENDING_PAYMENT, true);
$order->save();

Help: http://docs.magentocommerce.com/Mage_Sales/Mage_Sales_Model_Order.html