Jump to content

How To Change The Status Of An Order Programatically


Recommended Posts

Hi

 

I am developing a module in which I need to import and keep updated informations about orders that comes from an external server. I am having trouble changing the order status. The relevant part of the code that I am using is

            $current_order_state = $order->getCurrentOrderState();
            if ($current_order_state->id == $new_state->id || $current_order_state->module_name !== 'MYMODULENAME')
            {
                return true;
            }
            else
            {
                $history           = new OrderHistory();
                $history->id_order = $order->id;
                $use_existings_payment = !$order->hasInvoice();
                $history->changeIdOrderState((int) $new_state->id, $order, $use_existings_payment);
                if ($history->addWithemail(true, $templateVars))
                {
                    if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))
                    {
                        foreach ($order->getProducts() as $product)
                        {
                            if (StockAvailable::dependsOnStock($product['product_id']))
                            {
                                StockAvailable::synchronize($product['product_id'], (int) $product['id_shop']);
                            }
                        }
                    }

                }
                else
                {
                    Logger::addLog($this->l('Could not change status of order #%d.', $ps_order_id), 1);
                    return false;
                }
            }

but the status is just partially modified. When i open the AdminOrders controller, the order is with the right status:

 

post-769108-0-90153700-1455553884_thumb.png

 

But when i click at the order to see its detailed informations, the status is not correctly saved:

 

post-769108-0-48928000-1455554004_thumb.png

 

I checked in the database, and the order history is not being saved in the order_history table, but the method changeIdOrderState() does not trow any error. What am I doing wrong?

 

Link to comment
Share on other sites

Hi, -=peter=-, thanks for replying.

 

Yes, the code is copied from AdminOrdersController. I tried to use the debugger, but it does not helped me so much..

 

The most strange thing for me is that the method $history->addWithemail() (I tried $history->add() too) is returning success, but the history is not being created in the database. Any idea about this?

Link to comment
Share on other sites

I was able to solve the problem. 
 
When my module creates the order, I was using
$ps_order->current_state = $new_state_of_the_order;
$order->add();

and only after that, the code I have put in the first post was being executed, to create the $order_history for the order. So, the problem was that the order already had the status that I was trying to set to it, even though the $order_history had not been created yet. So, in the lines

$current_order_state = $order->getCurrentOrderState();
if ($current_order_state->id == $new_state->id || $current_order_state->module_name !== 'MYMODULENAME')
{
     return true;
}

$current_order_state and $new_state was the same, and the function was returing true without adding the $order_history.

 

The solution I used is to set the $order->current_state to 0 when creating the order, and updating the status to the correct one after this, when creating the $order_history.

 

I don't know if it is the best solution, but it seems to be working. Thanks for your help!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...