Set old quote Magento 2 cart
-
26-01-2021 - |
Question
I'm trying to load/set/restore an old quote from a customer when they log in, but the thing is not that simple, because it turns out that I'm using Cminds Multi User Account Extension, and the project required to create a user/account switcher when logging in as a Parent account and I've already got this covered, the issue that I have now is that all customers are sharing the same cart, and it happens when switching between accounts. I've fiddled with some checkoutSession
methods such as clearQuote()
, loadCustomerQuote()
and even trying to obtain from the quoteCollection, the last quote that the user had and using the replaceQuote($customerQuote)
method to replace it, but so far I've had no luck, the cart is still being the same for each customer (parent and childs) when I switch between them. Am I doing the wrong approach? is there an event that I can listen to with an observer? or is there a method that I can intervene using a plugin? Any help would be greatly appreciated.
Solution
In the case that anyone in the future might have this same problem or something related, I figured it out, it was actually a pretty simple thing I had to call the $this->checkoutSession->clearStorage()
and $this->checkoutSession->clearQuote()
before setting the customer as logged in, and after setting the customer as logged in I'll set the quote of the current logged-in customer with: $this->checkoutSession->loadCustomerQuote()
, this solved the first part, after this I was facing another problem, if I switched between users without adding products, the minicart info wouldn't change/update. To solve this other problem I added in the JS where I handled the customer info, and added:
require(['Magento_Customer/js/customer-data'], function(customerData ){
var sections = ['cart'];
customerData.invalidate(sections);
});
And after this the mini-cart updates. I hope this also helps someone else that's having the same problem I had.