Search code examples
prestashopprestashop-1.7prestashop-modules

Validate an order before is placed in Prestashop Module


I'm developing a Prestashop module to achieve this.

When an order with a specific product is placed I need to contact an external API to perform some write operations. To be more specific, some product will be automiatically ordered on a third party platform. It's not dropshipping, I'm sending the order to the producer of the goods.

The problem is, if the API call goes bad i must stop the order from being placed and notify the client that something has gone wrong, keeping the cart intact if possibile.

Everything else is working: i can detect the products, send the info to the third party api and read the response. Except I'm using a Module with this code:

$this->registerHook('actionValidateOrder');

It is my understanding this hook is called after an order is placed, so I can't block it if the API call goes wrong.

What is the right way to approach this scenario?

I tried to use actionOrderStatusPostUpdate but I don't think i can have prior knowledge on the order status (payment methods can vary).

Any idea? Seems like a basic task to accomplish but I'm stuck.

Using Prestashop 1.7.8


Solution

  • Do you need to validate the order? Because PrestaShop's order-related hooks are only available once the order has been created.

    Wouldn't a better solution be to check the cart when a product is added by the customer?

    hookActionCartSave
    

    In this case, you could avoid disappointed customers by letting them know at the time of adding the product to the basket that the products is not available.

    Update - Create a custom hook Unfortunately, there is no hook to stop the order from being validated and created. - but you can create one

    Register a new custom hook, for example actionValidateOrderBefore. Register the hook in your module:

    $this->registerHook('actionValidateOrderBefore')
    

    and exec the hook in the PaymentModule(classes/PaymentModule.php) class validateOrder method (public function validateOrder() ..)

      Hook::exec('actionValidateOrderBefore', [id_cart] => $id_cart);
    

    Reset your module, and you have you can manipulate the order before being created.

    If you want to do it very nicely, you can create an override for validateOrder method of the PaymentModul class https://devdocs.prestashop.com/1.7/modules/concepts/overrides/