On 3/8/2022 at 6:01 PM, Couponsva said:I know that, but I want to know why it's coming as null when product is added. Thanks
TLDR: check your current customer address, you must have one address (or default address) to get context->cart value
Just experienced the same problem, turns out its a prestashop bug (I'm on PS 1.7.8.8)
My problem case is, on hook ActionCartSave, when I var_dump(Context::getContext()->cart) is always null, and I cant get Context::getContext()->cart->getProducts(), will throw me error everytime. This happens only to logged in customer without any default address (no address at all)
Check on <ps root folder>/classes/controller/FrontController.php on init function, search for this line "if (!isset($cart) || !$cart->id) {" in my case its line 442. Just before that line, there is $cart->update, thats the one triggering actionCartSave (its trigger when ps check that the customer has no default address, and it will always be triggered until the customer has default address), and its triggered before initialization of the $this->context->cart (its inside the if on line 442).
Adding address on the customer solve the problem. but I wondering, if I have a heavy code on actionCartSave hook, turns out it will always executed everytime customer without address load a page (anywhere), it will slowdown the server.
I'll open this issue on github too, thanks
EDIT:
I solve the problem by overriding the FrontController, copying all init function with replacing this line
if ($to_update) {
$cart->update();
}
with
if ($to_update && Customer::getAddressesTotalById($cart->id_customer) > 0) {
$cart->update();
}
EDIT AGAIN:
it turns out very bad idea to override init function. So I just update the prestashop core. Very bad practice, but needed