Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nalysa 14 posts 124 karma points
    1 week ago
    Nalysa
    0

    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.

  • Matt Brailsford 2765 posts 14041 karma points MVP 7x c-trib
    1 week ago
    Matt Brailsford
    100

    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

    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);
    }
    

    Hope this helps

    Matt

  • Nalysa 14 posts 124 karma points
    1 week ago
    Nalysa
    0

    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

  • Matt Brailsford 2765 posts 14041 karma points MVP 7x c-trib
    1 week ago
    Matt Brailsford
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft