Jump to content

$this->context->cart is null when product is added into cart


Recommended Posts

You are requesting support for a module that is not the original Prestashop.
Not everyone has this module to give you advice.
When you turn off the module, uninstall it, is it still the same error?
Has Prestashop been upgraded?
If so, what tool did you use to update your Prestashop?

Link to comment
Share on other sites

Yes, I understand the case, but right now I'm trying to fix it at module end, 
 I need help in fixing the issue, why the $this->context->cart is coming null? When a product is added into the cart.

I'm fixing it my end not taking module support,

Hope you guys help me out here 

Link to comment
Share on other sites

8 minutes ago, ComGrafPL said:

Cart one. Also check hooks. Maybe you have other module, that could interfere add to cart.

Can you link your store here?

its on the local system, I'm doing the debugging.

-> Sendinblue module install
-> login as new customer
-> Add product into cart
Fatal error occur.

The hook is `hookActionCartSave`
I want to know why the $this->context->cart is coming null? 

 

Link to comment
Share on other sites

You should visit the Indian Forum, they will understand you there.
You have a few important questions that can solve your problem. You didn't answer either. Probably no one will advise you here anymore. You can start a Job and someone may solve your problem.You've dealt with it in more discussions. I apologize for my brief statement and for not being able to help.

Link to comment
Share on other sites

1 minute ago, knacky said:

You should visit the Indian Forum, they will understand you there.
You have a few important questions that can solve your problem. You didn't answer either. Probably no one will advise you here anymore. You can start a Job and someone may solve your problem.You've dealt with it in more discussions. I apologize for my brief statement and for not being able to help.

I understand @knacky Previously, other Prestashop support and community helped me here.
I debug the issue and I got it, why it's coming, where I got stuck is that why the `$this->context->cart` is null when the product is added into the cart. 
Is there anything wrong with current PS version or change modifications in ACtionCartSave hook?

Thats, all I want to know.

Link to comment
Share on other sites

Hello.
Why are you asking?
Do you present yourself as a developer and can't compare hooks in different versions of Prestashop?
Open Cart.php and CartController.php and you will find the difference.
Prestashop 1.7.8 works mainly via ajax and not all parameters can be null.
If you know the id cart is OK, the error is in the database or some other module has an override on the cart. Do you want a few hours of free explanations from the community and you will be paid for the repairs yourself?
There is documentation where all changes are written in Prestashop.
If you want to solve something, you have to read and study.
This is not how help works.

  • Like 1
Link to comment
Share on other sites

57 minutes ago, 4you.software said:

Hello.
Why are you asking?
Do you present yourself as a developer and can't compare hooks in different versions of Prestashop?
Open Cart.php and CartController.php and you will find the difference.
Prestashop 1.7.8 works mainly via ajax and not all parameters can be null.
If you know the id cart is OK, the error is in the database or some other module has an override on the cart. Do you want a few hours of free explanations from the community and you will be paid for the repairs yourself?
There is documentation where all changes are written in Prestashop.
If you want to solve something, you have to read and study.
This is not how help works.

@4you.software I've done all the above debug of comparing hooks, checking Cart.php and CartController.php, checking parameters, disabling other modules, I didn't get paid for anything, it's just as community I need some help to understand what I'm missing in my debugging. Thanks for you response 🙏🏻

Link to comment
Share on other sites

  • 1 month later...
On 1/24/2022 at 4:10 PM, knacky said:

You should visit the Indian Forum, they will understand you there.
You have a few important questions that can solve your problem. You didn't answer either. Probably no one will advise you here anymore. You can start a Job and someone may solve your problem.You've dealt with it in more discussions. I apologize for my brief statement and for not being able to help.

I know that, but I want to know why it's coming as null when product is added. Thanks

 

Link to comment
Share on other sites

  • 1 year later...
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 :( 

Edited by casualPScoder
update my fixes (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...