Jump to content

actionValidateOrder vs. actionPaymentConfirmation


chrisdac

Recommended Posts

Hi,

 

I'm just getting started with Prestashop and want to add some custom code to be executed once a customer has successfully completed an order.

 

It seems like the correct thing to do here would be to create a module and associate it with the relevant hook; there seem to be two hooks that might be suitable: actionValidateOrder and actionPaymentConfirmation.

 

My question is: which is the most appropriate hook to use and under what circumstances?

 

Thanks,

 

Chris

Link to comment
Share on other sites

actionPaymentConfirmation will be executed anytime the order status is changed to "Payment Accepted"

 

actionValidateOrder occurs directly after payment is submitted and an order is created, but before the email is sent to the customer.

 

In both cases, and order is successfully created. You will need to clarify if payment needs to be successful as well.

  • Like 1
Link to comment
Share on other sites

  • 9 years later...

Hi! (1.7.5. upgraded to 1.7.8.7)

I am trying to figure out how to change an order status to paid (2|Payment Acccepted) so that ALL DB entries are created and not just the ps_orders.current_state is updated.

After some digging, there are a lot of tables that are updated when you manually set order as paid in the BO.

Does actionValidateOrder, actionPaymentConfirmation or some other hook create all entries in the database and send the email as changing the status manually from the BO would do?

Also, can I create a simple PHP script and call these hooks from that, or can they only be called from a registered module?

 

Edited by GoPure Kratom (see edit history)
Link to comment
Share on other sites

You can use the below code to update the order status with all DB entries and mails.

 

$kb_order_obj = new Order({Order ID});                                //Relace {Order ID} with order ID
$history = new OrderHistory();

$history->id_order = $kb_order_obj->id;
$history->id_employee = (int) $this->context->employee->id;
$use_existings_payment = false;
if (!$kb_order_obj->hasInvoice()) {
         $use_existings_payment = true;
}
$history->changeIdOrderState((int){Order state} , $kb_order_obj, $use_existings_payment);                              //{Order state} with updated state ID
             
$carrier = new Carrier($kb_order_obj->id_carrier, $kb_order_obj->id_lang);
$templateVars = array();
       if ($history->id_order_state == Configuration::get('PS_OS_SHIPPING') && $kb_order_obj->shipping_number) {
       $templateVars = array(
                        '{followup}' => str_replace('@', $kb_order_obj->shipping_number, $carrier->url)
                    );
                }
                if ($history->addWithemail(true, $templateVars)) {
                    // synchronizes quantities if needed..
                    if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) {
                        foreach ($kb_order_obj->getProducts() as $product) {
                            if (StockAvailable::dependsOnStock($product['product_id'])) {
                                StockAvailable::synchronize(
                                    $product['product_id'],
                                    (int) $product['id_shop']
                                );
                            }
                        }
                    }
                }

Link to comment
Share on other sites

Thanks a LOT, awesome! I'm trying to make it work but have a couple of questions.

On the line below where the order status is changed, the code chokes on an exception, although the order's status gets updated(?!), but the execution is terminated to this line.

$history->changeIdOrderState((int)$order_state , $kb_order_obj, $use_existings_payment);

I'm trying to pass 2 as the $order_state which in my installation is the Payment Accepted.

PHP Notice:  Trying to get property 'precision' of non-object in /path/public_html/classes/Context.php on line 498
PHP Fatal error:  Uncaught TypeError: Argument 1 passed to PrestaShop\PrestaShop\Core\Localization\CLDR\ComputingPrecision::getPrecision() must be of the type int, null given, called in /path/public_html/classes/Context.php on line 498 and defined in /path/public_html/src/Core/Localization/CLDR/ComputingPrecision.php:41
Stack trace:
#0 /path/public_html/classes/Context.php(498): PrestaShop\PrestaShop\Core\Localization\CLDR\ComputingPrecision->getPrecision()
#1 /path/public_html/classes/order/Order.php(1465): ContextCore->getComputingPrecision()
#2 /path/public_html/classes/order/Order.php(1353): OrderCore->setInvoiceDetails()
#3 /path/public_html/classes/order/OrderHistory.php(371): OrderCore->setInvoice()
#4 /path/public_html/awtest.php(19): OrderHistoryCore->changeIdOrderState( in /path/public_html/src/Core/Localization/CLDR/ComputingPrecision.php on line 41

I think it has something to do with the first argument. is there something wrong witht he casting? I've tried plain number, variable, casted/uncasted.
I've checked the files in the stacktrace, and parameters and types passed seem to be ok to me.

Not sure if it matters, but I'm currently running this as a "standalone" php script and not as Presta standard file etc.

 

Here's the whole code:

<?php

require(dirname(__FILE__).'/config/config.inc.php');

$order_state = 2; //Payment Accepted
$bot_employee_id = 14;

$kb_order_obj = new Order(4617);
$history = new OrderHistory();

$history->id_order = $kb_order_obj->id;
$history->id_employee = (int)$bot_employee_id; // $this->context->employee->id;

$use_existings_payment = false;
if (!$kb_order_obj->hasInvoice()) {
         $use_existings_payment = true;
}

$history->changeIdOrderState((int)$order_state , $kb_order_obj, $use_existings_payment); 

// $carrier = new Carrier($kb_order_obj->id_carrier, $kb_order_obj->id_lang);

// $templateVars = array();

// if ($history->id_order_state == Configuration::get('PS_OS_SHIPPING') && $kb_order_obj->shipping_number) {
//     $templateVars = array('{followup}' => str_replace('@', $kb_order_obj->shipping_number, $carrier->url));
// }

// if ($history->addWithemail(true, $templateVars)) {
//     // synchronizes quantities if needed..
//     if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) {
//         foreach ($kb_order_obj->getProducts() as $product) {
//             if (StockAvailable::dependsOnStock($product['product_id'])) {
//                 StockAvailable::synchronize($product['product_id'],(int) $product['id_shop']);
//             }
//         }
//     }
// }

echo "Order ID: ".$kb_order_obj->id."\n";
echo "Order reference: ".$kb_order_obj->reference."\n";

?>

 

Edited by GoPure Kratom (see edit history)
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...