Merging Carts When A Customer Logs In: Problems, Solutions and Recommendations
Today I would like to look into the case when a customer, having items in their authenticated shopping cart, but browsing the site anonymously, as a guest, creates a new shopping cart, and then log in. The question is what should happen to the items in the account-linked carts versus the items in the anonymous carts.
For merchants, there is a reason behind merging carts, and that is in wanting the customers not to forget the products they intended to buy earlier. From the customer’s perspective, having extra items in the cart after logging in may be confusing and undesired, especially if done during the checkout process and especially if the shopping carts are large and complex.
There are five possible solutions:
- Non-interactive
- “Session cart priority”. If a customer has an anonymous cart, the account-linked shopping cart will be ignored after the login. If an anonymous cart is empty, the shopping cart contains the items from the account-linked cart.
- “Cart Archive”: Replacing the old cart with the latest cart; moving the contents of the old cart to the wish list or archived carts list and inform the user about the actions done,
- “Merge”Combine the shopping carts together so the user still has a single shopping cart, and the merged cart replaces the old account-linked cart, and inform the user about the changes,
- “Multiple carts”: Merging carts by default, but letting the customer undo the operation and restore one of two saved carts, “anonymous/guest” and “old account-linked”.
- Interactive:
- “Interactive” Explaining the situation to the customer and letting him decide what he wants to do, namely, which cart to keep or if he wants to merge them:
- You have items both in your present cart. But you also have items stored in a previous cart. Would you like to:
- with your present items
- your saved items into your present purchase
- You have items both in your present cart. But you also have items stored in a previous cart. Would you like to:
- “Interactive” Explaining the situation to the customer and letting him decide what he wants to do, namely, which cart to keep or if he wants to merge them:
- leaving the account-linked cart doesn’t seem right because of security and privacy considerations and
- restoring the anonymous cart is not correct too, because one of the products from the original cart or the whole cart can be already purchased by the end of the customer session
SAP Commerce Out-of-the-box Merging Carts Strategy: Architecture and implementation details
To use mergingCartRestorationStrategy- create a your-customized-addon extension
- add the following code in the your-customized-addon-web-spring.xml <alias name=”mergingCartRestorationStrategy” alias=”cartRestorationStrategy”/>
- Install the addon via ant addoninstall -Daddonnames=”customized-addon extension” -DaddonStorefront.yacceleratorstorefront=”your acceleratorstorefront”
- Recreates a new session cart (let’s name it “B*” from the current session cart (B)
- Removes all payment transactions linked to the cart B
- Re-generates cart GUID for B*
- Recalculates the cart B* (entries, totals, discounts, taxes…) via commerceCartCalculationStrategy
- If any entries aren’t available, they won’t be added
- Updates promotions on B*
- Executing cart calculation hooks (“before” and “after”) on B*
- ●Recalculates the cart (B*) again (entries, totals, discounts, taxes…) via commerceCartCalculationStrategy. This time for the new cart object which is a duplicate of the original session cart.
- Update promotions again on B*. This time for the new cart object which is a duplicate of the original session cart.
- Calculates external taxes if needed
- Merges a recreated session cart (B*) with an account-linked cart (A, the most recent if more than one)
- Special processing for entry groups (if any)
- Move all cart entries from the account-linked cart (A) to the current session cart (B*)
- Remove old session cart (B)
- The merged cart is a union of A and B, C=A+B*;
- Recalculates the resulting cart (A+B*)
- Calculate (A+B*) cart [again] via commerceCartCalculationStrategy
- Update promotions again on A+B*.
- Prerequisites:
- Account-linked cart – 6 items
- Anonymous cart – 1 item
- Scenario:
- A customer signs in having 6 items in the account-linked cart and 1 item in the session cart.
- Totally, after the customer logged in, the following methods are called six times (!):
- calculateEntries, calculateTotals, updatePromotions – 6 times
- First – From onAuthenticationSuccess → loginSuccess → updateSessionCurrency → setSessionCurrency -> recalculateCart → calculateEntries
- Second – From onAuthenticationSuccess → loginSuccess -> recalculateCart → calculateEntries
- Third – From onAuthenticationSuccess → restoreCart (mergingCartRestorationStrategy) → restoreCartAndMerge → restoreCart (service) → restoreCart (DefaultCommerceCartRestorationStrategy) → recalculateCart → calculateEntries
- Fourth – From onAuthenticationSuccess → restoreCart (mergingCartRestorationStrategy) → restoreCartAndMerge → restoreCart (service) → restoreCart (DefaultCommerceCartRestorationStrategy) → calculateCart → calculateEntries
- Five – From onAuthenticationSuccess → restoreCart (mergingCartRestorationStrategy) → restoreCartAndMerge → restoreCart (service) → restoreCart (DefaultCommerceCartRestorationStrategy) → recalculateCart → recalculate → calculateEntries
- Six – From onAuthenticationSuccess → restoreCart (mergingCartRestorationStrategy) → restoreCartAndMerge → restoreCart (service) → restoreCart (DefaultCommerceCartRestorationStrategy) → recalculateCart → calculate → calculateEntries
- calculateEntries, calculateTotals, updatePromotions – 6 times