Accessing shoppingcart through user or organization?

Hi,
How do I get hold of the shoppingcart using either user or organization (or their IDs)?
Currentstate.Current.Shoppingcart.Orderrow has no orderrows atm and I need to put them back.

Thanks

:slight_smile: Philippe

Litium version: 5.6.8

What do you mean when you say “get hold of the shopping cart”?

Do you mean like a saved cart for a user or a cart from an earlier order belonging to a user?

What’s the use case?

I’ve created a cookie to keep user logged in so when the session expires the cookie goes in and logs the user in again on seesion_begin. All works well expect for their Shoppingcart that doesn’t load properly and I can’t reach it using currentstate.current. Logging out and in again solves this issue and shows the proper shopppingcart again, however I don’t want the customers to have to do that. They should be able to work just like they did before lunch or weekend or whatever time has elapsed.

Not sure if I should save the shoppingcart too in a cookie but then again, Litium has already saved it somewhere.

If you are re-logging in the Customer then the ShoppingCart should be reloaded. Describe in code how you are re-logging in the user please!

Yes, that may be the reason. Perhaps I have not fully logged in the user…
What I do is to create a cookie upon loggin in with userId and OrganizationId.
Then on Session_Start in Global.asax.cs I have written the folowing code:

        if (Request.Cookies["_brlg"] != null)
        {
            var cookie = Request.Cookies["_brlg"].Value.Split('|');
            var userId = new Guid(cookie[0]);
            var organizationId = new Guid(cookie[1]);
            var organization = ModuleRelations.Instance.Organizations.GetOrganization(organizationId);
            var user = Solution.Instance.Users.GetUser(userId);
            if (organization != null && user != null)
            {
                var claimsIdentity = Solution.Instance.LoginManager.GetClaimsIdentity(new ApplicationUser(user.ID, user.LoginName));
                var token = new SecurityToken(claimsIdentity);
                Solution.Instance.LoginManager.Login(token);
                Session["SelectedOrganization"] = organization.ID;
                PersonStorage.CurrentSelectedOrganization = organization;

however this part is not funciontioning:

var orderCarrier = CurrentState.Current.ShoppingCart.OrderCarrier;

It creates a new ShoppingCart for me thus my question was if I could retreive it another way with userId or OrganizationId instead of currentState

Thanks for helping me out. Much appriciated :slight_smile:

ps,
I may also add that if I log out and in again the Shoppingcart will have returned…

You’ve enabled Save shopping cart information for signed in users only in Settings > Sales?

I set up a simpler version of your code, when I debug it and inspect orderCarrier I can see my rows.

// took user GUID from db for testing
var user = Solution.Instance.Users.GetUser(new Guid("DDA4A13E-5E7C-4DAF-A2BD-99D640F296AA"));
var claimsIdentity = Solution.Instance.LoginManager.GetClaimsIdentity(new ApplicationUser(user.ID, user.LoginName));
var token = new SecurityToken(claimsIdentity);
Solution.Instance.LoginManager.Login(token);

var orderCarrier = CurrentState.Current.ShoppingCart.OrderCarrier;

Here’s how I did it:

  1. Start a new private browser session
  2. Visit page and login as the same user I’m testing with
  3. Add some products to cart
  4. Log out
  5. Close browser and start new private browser session
  6. Run code from example (via test controller)

Yes, it is set in backoffice. And it do work when loggin in and out the ordinary way. But when timed out I can’t get the new CurrentState synchronized…
You mentioned rowS. Thus you have orderrows.count > 0? Cause I can see an orderrow but it has no values.
I will try with your Guid-example but it seams like exactly the same code I’ve written…

Thanks for putting some time to this :slight_smile:

I hope that you not plan to use this example code in production due to the large security issue to not encrypt information with environment unique security keys. Otherwise anyone that have got this cookie with a valid organization id can change the last part to the system user id and next restart of the browser will grant them full permissions to the system.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.