trustpilot / plugin-prestashop

Prestashop plugin for sending invitation data to Trustpilot & adding TrustBoxes
https://www.trustpilot.com/
MIT License
2 stars 4 forks source link

Error on hookActionOrderHistoryAddAfter if no cart is present #5

Open esbenboye opened 3 years ago

esbenboye commented 3 years ago

Hi,

I'm having an issue with this plugin. If changes are made to an order status and no cart is present, it throws an error about trying to get the property of a non-object.

How to reproduce?

Error is thrown

[Symfony\Component\Debug\Exception\ContextErrorException code 0]: Notice: Trying to get property 'id_lang' of non-object

I've traced it back to the hookActionOrderHistoryAddAfter method of trustpilot.php. The code expects $params['cart'] to be present, but it is not.

I have only worked with Prestashop for a short time, but as far as I can tell, the following code should resolve the issue by first checking if the cart is present in the event, and if it is not, then use the id_lang of the order associated with the order history.

public function hookActionOrderHistoryAddAfter($params)
{
    $id_lang = $params['cart'] ?
        $params['cart']->id_lang :
        (new Order($params['order_history']->id_order))->id_lang;

    $newOrderStatus = new OrderState((int) $params['order_history']->id_order_state, (int) $id_lang);
    $transformedParams = array('newOrderStatus' => $newOrderStatus, 'id_order' => $params['order_history']->id_order);
    $this->sendBackendInvitation($transformedParams, 'actionOrderHistoryAddAfter');
}

I have not submitted a PR since I'm not sure if this is the best solution.