Prestashop - Change order status when payment is validated
Asked Answered
B

3

5

When a payment is validated, the order status becomes "Payment validated" ("Paiement accepté" in french). I want to set another status when payment is validated, so the history would show the following :

Current status : My personnal status
History :
My personnal status
Payment validated

To do so, I use the hook actionOrderStatusPostUpdate. This is my code :

public function hookActionOrderStatusPostUpdate($aParams) {
    $oOrder = new Order($aParams['id_order']);

    if($aParams['newOrderStatus']->id == Configuration::get('PS_OS_PAYMENT')) {
        $oOrder->setCurrentState(Configuration::get('XXXXXX_STATUS_WAITING'));
        $oOrder->save();
    }
}

The Configuration values are correctly defined. This code works, because I see the status changed. But the thing is it changed BEFORE changing to "Payment validated". I don't understand why. The history looks like this :

Current status : Payment validated
History :
Payment validated
My personnal status

What should I do to make My personnal status appear as the last status ?

Boule answered 9/2, 2015 at 11:24 Comment(6)
Can you give some screenshots please, I can't understand what do you mean.Ineluctable
Screenshot : link. The status "Paiement accepté" (which is the french for Payment validated) is the last registered. The other one, "À produire par le fablab" is the one I would like to be last.Boule
This is a screenshot of order history page? right? Do you want "À produire par le fablab" before "Paiement accepté"?Ineluctable
Yes, this is what I want. Also, Configuration::get('XXXXXX_STATUS_WAITING') gives the id of the status "À produire par le fablab", and Configuration::get('PS_OS_PAYMENT') gives the id of the status "Paiement accepté".Boule
Ok, I understand. Please can you post your validateOrder method?Ineluctable
validateOrder is the one that calls the hook actionValidateOrder, so I doubt it's the one you need. Anyways, here it is : link. And here is the method changeIdOrderState that calls the hook I use (hookActionOrderStatusPostUpdate) : link. I believe these are the unchanged prestashop methods.Boule
S
6

hookActionOrderStatusPostUpdate hook call is made by changeIdOrderState but the add to order_history table is made after the call of changeIdOrderState like in https://github.com/PrestaShop/PrestaShop/blob/1.6.1.x/controllers/admin/AdminOrdersController.php#L521-L542

You rather need to bind your module on a classic hook like hookActionObjectOrderHistoryAddAfter https://github.com/PrestaShop/PrestaShop/blob/1.6.1.x/classes/ObjectModel.php#L535-L537

public function hookActionObjectOrderHistoryAddAfter($params) {
$orderHistory = $params['object'];

if($orderHistory->id_order_state == Configuration::get('PS_OS_PAYMENT')) {
    $oOrder->setCurrentState(Configuration::get('XXXXXX_STATUS_WAITING'));
    $oOrder->save();
}

Best Regards

Synopsize answered 14/7, 2017 at 16:49 Comment(4)
Even though this question is from two years ago, I was still wondering how to fix that thing. And there you come up with a perfect answer. Thank you :D !Boule
You're welcome @YannBergonzat Yes it is because I was faced with the same problem and I thought that the solution could be useful for those who would fall on this topic ... even in 2017 :)Synopsize
Although the answer is correct, there a major typo in the hook name it should be hookActionOrderHistoryAddAfter (without object), since this is a dynamic hook (doc.prestashop.com/display/PS15/…) and the object name is "OrderHistory". You also forget to remind that this hook should be registered at install time using $this->registerHook('actionOrderHistoryAddAfter');Puton
No, I maintain that hookActionObjectOrderHistoryAddAfter is the right answer. Take a closer look at the doc, the dynamic hook is composed like that : actionObject + ObjectName + AddAfter and/or see directly the source code github.com/PrestaShop/PrestaShop/blob/develop/classes/…Synopsize
N
1

I think this is what you should use to change order status after payment validate those hooks are called when status changing or status changed.

$history = new OrderHistory();
$history->id_order = (int)$id_order;
$history->changeIdOrderState($status_id, (int)$id_order);
$history->addWithemail();
$history->save();
Neuritis answered 19/3, 2019 at 8:6 Comment(0)
P
0

I think it'll work on other hook: actionOrderStatusUpdate

Pulsatory answered 9/11, 2015 at 13:2 Comment(2)
Already tried it, it didn't work either... The status was applied before the status update. That's why I chose to use the "post update" one.Boule
Maybe a silly question but normally on the BO the history displays the order statuses in reverse order, are you looking there or directly on the mysql? In the picture we can see the minute but not the seconds.Pulsatory

© 2022 - 2024 — McMap. All rights reserved.