Session Lifecycle Hooks

Is it possible to add hooks to Kirby session lifecycle events?

I’m implementing an e-commerce and the cart is stored into the user session.
I would like to lock items in the cart so that they are no longer available in the shop, but put them back in stock if an order is not placed and the cart lifetime has ended.

Since I conflate cart with user session, this means being able to know when a session is being deleted.

No, that’s unfortunately not possible as sessions that just expire are not deleted immediately, but at some point in the future by the garbage collector (as Kirby does not run a timer in the background that will delete the file right after it has expired).

For your use-case I’d suggest to create an index that maps all items that are in carts to the session they belong to. Each time you want to determine the available stock, you can then check which of the sessions still exist.

Thanks for your answer!
In the end I figured removing the items from stock once they are put in the cart is overkill as I assume people putting something in the cart but not submitting the order isn’t so rare.
So I only remove from stock when an order is submitted (and payment may still be pending, but still).

1 Like