phpprestashop

Prestashop Auto Change Statut if customer in whitelist


I have customers that are allow to pay after shipment 30 days. For logistic purposes i added a new status "Shippment before payment". I have to place them manually.

So i try to make a prestashop module where i add customers id in a whitelist and when the customer make an order and if he's in the whitelist and the payment method is wire payment then we change the statut to "Shippment before payment"

I'm a python dev, i'm not familiar with PHP so i tried differente things and i think i'm in a good way, but my status change correctly but not in the correct order. He first change in "Shippmment before payment" and after that change, he switch to 'Waiting for Wire tranfert'.

<- Here is what it's looking on status changes ->

There is my hook :

public function install()
 {
    Configuration::updateValue('ORDER_STATUS_LIVE_MODE', false);

    return parent::install() &&
        $this->registerHook('header') &&
        $this->registerHook('displayBackOfficeHeader') &&
        // $this->registerHook('actionOrderStatusUpdate');
        // $this->registerHook('actionValidateOrder');
        $this->registerHook('actionOrderStatusPostUpdate');
 }

And my actual function :

    public function hookActionOrderStatusPostUpdate($params)
    {
        static $alreadyRun = false;

        if ($alreadyRun) {
            return;
        }
        $this->logToFile("Début hookActionValidateOrder");

        $orderId = $params['id_order'];
        $order = new Order($orderId);
        $customer = new Customer($order->id_customer);
        $this->logToFile("Commande ID: {$order->id}, Client ID: {$customer->id}, Module de paiement: {$order->module}");

        $customStatusId = 20; // Ici l'ID de mon statut
        $whitelist = explode(',', Configuration::get('ORDER_STATUS_WHITELIST'));

        // Vérifiez si le client est dans la whitelist et que le statut actuel est "En Attente de Virement Bancaire"
        if (in_array($customer->id, $whitelist) && $order->current_state == Configuration::get('PS_OS_BANKWIRE')) {
            if ($order->module == 'ps_wirepayment') {
                $this->logToFile("Commande {$orderId} de client whitelisté, mise à jour du statut à {$customStatusId}");
                $order->setCurrentState($customStatusId);
                $order->save();
                $alreadyRun = true;
            }
        }
    }

    private function logToFile($message)
    {
        $logPath = $this->local_path . 'log/order_status_debug_6.log';
        $date = new DateTime();
        $timestamp = $date->format('Y-m-d H:i:s');
        $logEntry = "[$timestamp] $message\n";
        if (!is_dir(dirname($logPath))) {
            mkdir(dirname($logPath), 0755, true);
        }
        file_put_contents($logPath, $logEntry, FILE_APPEND);
    }

If any PrestaShop Expert could help :D

Thanks ! Morgan


Solution

  • You cannot use this hook in your case because hookActionOrderStatusPostUpdate is called while the order status is being changed. So what you are doing is changing the status before the previous change is even finalized.

    You should use hookActionOrderHistoryAddAfter (called after a new Order status is saved) instead and adapt your code like this:

    public function hookActionOrderHistoryAddAfter($params){
        static $alreadyRun = false;
    
        if ($alreadyRun) {
            return;
        }
        $this->logToFile("Début hookActionValidateOrder");
    
        $orderHistory = $params['order_history'];
        $orderId = $orderHistory->id_order;
        $order = new Order($orderId);
        $customer = new Customer($order->id_customer);
        $this->logToFile("Commande ID: {$order->id}, Client ID: {$customer->id}, Module de paiement: {$order->module}");
    
        $customStatusId = 20; // Ici l'ID de mon statut
        $whitelist = explode(',', Configuration::get('ORDER_STATUS_WHITELIST'));
    
        // Vérifiez si le client est dans la whitelist et que le statut actuel est "En Attente de Virement Bancaire"
        if (in_array($customer->id, $whitelist) && $orderHistory->id_order_state == Configuration::get('PS_OS_BANKWIRE')) {
            if ($order->module == 'ps_wirepayment') {
                $this->logToFile("Commande {$orderId} de client whitelisté, mise à jour du statut à {$customStatusId}");
                $order->setCurrentState($customStatusId);
                // $order->save();
                $alreadyRun = true;
            }
        }
    }
    

    I have commented $order->save() at the end, because it is not necessary here.