agjjr Posted February 15, 2016 Share Posted February 15, 2016 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: But when i click at the order to see its detailed informations, the status is not correctly saved: 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 More sharing options...
-=peter=- Posted February 16, 2016 Share Posted February 16, 2016 The code is copied from AdminOrdersController? Always use debugger to solve problems like this.. Link to comment Share on other sites More sharing options...
agjjr Posted February 17, 2016 Author Share Posted February 17, 2016 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 More sharing options...
-=peter=- Posted February 17, 2016 Share Posted February 17, 2016 It's hard to say, but I used $order->setCurrentState() in my plugin and it worked. Do you start a db transaction somewhere in your code? Link to comment Share on other sites More sharing options...
agjjr Posted February 19, 2016 Author Share Posted February 19, 2016 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now