Clearing Cart after session expiry

Hi Team,

I am trying to clear the cart after session expires. Could you please assist me how can i achieve this.

Thanks,
Arun

Hi Arun,

I would suggest to implement the delete method in your CartRepository and clear you cart directly there.
You can invoke that method once the session expires.

HTH,
Giacomo

1 Like

Thanks for your response Giacomo.

I have followed https://documentation.bloomreach.com/library/concepts/login/active-logout.html and here mentioned for cms and console. Is “site” comes under cms?

When session expires storeUser has been removing from the session. could you please let me know the session configuration.

Thanks,
Arun

Hi,
the active logout you mentioned has nothing to do with the site application, and more specifically with the Starter Store.

When the user logs out, the storeUser object is removed from the session. You can see this in the LoginStatusComponent: maybe you can extend that component and trigger the cart deletion from there.

HTH,
Giacomo

giacomolm
July 17

Hi,
the active logout you mentioned has nothing to do with the site application, and more specifically with the Starter Store.

When the user logs out, the storeUser object is removed from the session. You can see this in the LoginStatusComponent: maybe you can extend that component and trigger the cart deletion from there.

Shouldn’t this be a generic starter store feature? I assume that when
the session expires, a cart always should be deleted isn’t?

Regards Ard

No. Giacomo is right. It should be implemented in the CartRepository to remove the old cart if necessary because cart data is maintained by the Commerce Backend Platform such as Commercetools or ElasticPath, not in StarterStore application.


woonsanko

    July 17

No. Giacomo is right. It should be implemented in the CartRepository to remove the old cart if necessary because cart data is maintained by the Commerce Backend Platform such as Commercetools or ElasticPath, not in StarterStore application.

Just out of curiosity: how is the coupling from a visitor to the commerce backend done? I assume that in the end it is done via the http session of the visitor isn’t? Then when that http session invalidates, it should be possible to also remove the old cart from the commerce backend I’d think. Or do I misunderstand how the visitor is tied to her cart?

Regards Ard

Hi,

I have extended the LoginStatusComponent and it is working as expected but it is an action component. When user clicks on logout this will trigger and does the logout actions. In same LoginStatusComponent i have assigned cartCookie maxAge to 0(zero) and added that back to response, it’s working fine.

Now i have to make it work same when session expires.

  1. If session expires LoginStatusComponent is not triggering(let me know the configuration if something missed)
  2. Current functionality is I can still view cart after session times out. It should be cleared.

Thanks,
Arun

When user signs in, she should be able to retrieve her cart data regardless of http session. So, the cart is normally provided by the backend system.

Could you please help me to understand how logout functionality works for starter store when session times out.

I guess you want to expire any visitor’s cart if the visitor’s http session expires. Is that right?
You don’t need to keep the visitor’s cart when the visitor revisit the site in a while?

Anyway, at the moment, cart cookie is not synchronized with http session lifecycle. Cart cookie has a separate lifecycle, a week by default.

If you want a feature to optionally support synchronizing cart cookie with visitor’s http session, please create a JIRA ticket (through your project specific jira project for now).

FYI, there are two locations adding the cart cookie: CartCreateCommand and AbstractStarterStoreResource, both of which sets 7 day expiration.
It seems like a nice improvement to make it configurable. e.g, 7d by default, but -1 for your use case as an example.

Regards,

Woonsan


woonsanko

    July 17

arun.bandi:
I have extended the LoginStatusComponent and it is working as expected but it is an action component. When user clicks on logout this will trigger and does the logout actions. In same LoginStatusComponent i have assigned cartCookie maxAge to 0(zero) and added that back to response, it’s working fine.

Now i have to make it work same when session expires.

I guess you want to expire any visitor’s cart if the visitor’s http session expires. Is that right?

You don’t need to keep the visitor’s cart when the visitor revisit the site in a while?

Anyway, at the moment, cart cookie is not synchronized with http session lifecycle. Cart cookie has a separate lifecycle, a week by default.

Who writes the Cart Cookie? I assume we do that right? Would it make sense to have a boolean flag whether the cart cookie is ‘tight’ to the http session or not? If it is tight to the http session, it means that it needs to be emptied in case it isn’t connected to the ‘current http session’. This can be achieved easily by on the cart cookie store an extra value, some UUID. Store this same UUID as a key on the http session. For a request, when there is a cart cookie, always validate the UUID against the http session attribute holding the UUID. If they are not the same, it means the http session has been (re)created and the previous one thus invalidated : Therefor the cart cookie has to be removed / reset. As said, this ‘binding’ of the cart cookie to http session can be a configurable boolean

Regards Ard

Hey Ard!
The main reason why the Starter Store introduced this cookie (cartId) was because of anonymous cart support: in this specific case StarterStore visitors don’t need to authenticate if they want to add products to the cart. As a consequence, the StarterStore needs to store the anonymous cartId.

In case of “authenticated” carts, the StarterStore doesn’t need to handle those kind of information (including the cartId). Cart information can be retrieved using for example the visitor’s username: but more generally, it’s up to the commerce backend what to do in case the user’s session expires.

If that’s ok for you, I would like to file a documentation improvement here [1]: hopefully things will be more clear in the future.

In any case, my suggestion is aligned to the general idea where any StarterStore based application should try to delegate commerce specific operation to the commerce backend: in this specific case, less is more :slight_smile:

[1] https://documentation.bloomreach.com/library/solutions/commerce-starterstore/component-libraries/checkout-components.html

Hey Giacomo,

thanks for your explanation, makes sense to me!

Regards Ard