When a customer logs in on the checkout page in Magento 2, the system combines the existing cart items with the previous abandoned cart items, resulting in a non-optimal customer experience.

Default Behavior in Magento 2

By default, Magento 2 combines the previous abandoned cart items with the current cart items upon customer login on the checkout page.

Desired Behavior

The desired behavior is to remove the previous abandoned cart items and retain only the current items when a customer logs in while on the checkout page, distinct from the sale_quote_merge events.

Solution Approach

To achieve this, it is necessary to observe the customer_login event and handle the removal of abandoned cart items during this event.

This modification should exclude the action on sale_quote_merge events, both before and after.

Implementing the Solution

Create a custom module in Magento 2 to observe the customer_login event using event.xml.

Implement a custom PHP class that handles the removal of abandoned cart items on the customer_login event.

Utilize the appropriate methods to remove the abandoned cart items from the quote when the customer logs in.

Ensure that the functionality does not interfere with the sale_quote_merge events.

Conclusion

By implementing a custom module to observe the customer_login event and handling the removal of abandoned cart items, you can ensure that only the current cart items are retained when a customer logs in on the checkout page, providing an improved user experience.

This approach allows for flexibility and customization while maintaining the integrity of the Magento 2 checkout process.