Prestashop - Change order status when payment is validated

天涯浪子 提交于 2019-12-09 18:33:35

问题


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 ?


回答1:


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




回答2:


I think it'll work on other hook: actionOrderStatusUpdate




回答3:


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();


来源:https://stackoverflow.com/questions/28408612/prestashop-change-order-status-when-payment-is-validated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!