We have a store and the user must be logged in first to add products to the cart. However, how to attach the cart to each users so when the user login to the website, they will have their own cart? We've tried with GetOrCreateCurrentOrder(storeId, customerReference) but when the user login from another browser, it'll create a new cart instead of updating the existing cart.
The current order (or cart) is only tracked via a cookie so is only kept on the one browser session. Accessing the site from another browser will currently create a new order.
The best I could suggest would be to maybe extend your login logic to look for open orders for the current member and if one is found, set this to the current order in the session manager.
I created a helper method for another issue that might come in handy here which you can find on this gist https://gist.github.com/mattbrailsford/a9cc7768756ec87e30fa4494820bec27 Essentially this will fetch all orders for a member, finalized or not. If you wanted to, you could modify this to bring back un-finalized orders by adding the following to the query statement
AND finalizedDate IS NULL
This should then give you all the open orders for the current member. You'll probably then need to identify which order is the latest one, and then you can put it into the session for the current user, so something like this.
var openOrder = _orderService.GetOpenOrdersForCustomer(storeId, member.Key.ToString())
.OrderByDescending(x => x.CreateDate)
.FirstOrDefault();
if (openOrder != null) {
_sessionManager.SetCurrentOrder(storeId, openOrder);
}
Thank you for your suggestion, it works as we expected!
For your information, we end up implementing this when the user logs in, adds a product to the cart, and opens the cart page, to ensure that the cart is always updated.
I never thought about orders being transfered between sessions. I'll add it to our issue tracker as a feature enhancement to review, but it's good we have a workaround in the meantime.
Attach Cart to Member
Hi,
We have a store and the user must be logged in first to add products to the cart. However, how to attach the cart to each users so when the user login to the website, they will have their own cart? We've tried with
GetOrCreateCurrentOrder(storeId, customerReference)
but when the user login from another browser, it'll create a new cart instead of updating the existing cart.Thanks.
Hi Nalysa,
The current order (or cart) is only tracked via a cookie so is only kept on the one browser session. Accessing the site from another browser will currently create a new order.
The best I could suggest would be to maybe extend your login logic to look for open orders for the current member and if one is found, set this to the current order in the session manager.
I created a helper method for another issue that might come in handy here which you can find on this gist https://gist.github.com/mattbrailsford/a9cc7768756ec87e30fa4494820bec27 Essentially this will fetch all orders for a member, finalized or not. If you wanted to, you could modify this to bring back un-finalized orders by adding the following to the query statement
This should then give you all the open orders for the current member. You'll probably then need to identify which order is the latest one, and then you can put it into the session for the current user, so something like this.
Hope this helps
Matt
Hi Matt,
Thank you for your suggestion, it works as we expected!
For your information, we end up implementing this when the user logs in, adds a product to the cart, and opens the cart page, to ensure that the cart is always updated.
Best regards
Hi Nalysa,
That's great!
I never thought about orders being transfered between sessions. I'll add it to our issue tracker as a feature enhancement to review, but it's good we have a workaround in the meantime.
Matt
is working on a reply...