How do I stop Magento merging the basket on login?

How do I stop Magento merging the basket on login?

July 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How do I stop Magento merging the basket on login?

Solving the Magento Basket Merging Issue on Login

Have you ever experienced the frustration of logging into your Magento store only to find that your carefully selected products have been merged with an old saved basket? This common issue arises when the system attempts to combine a guest user’s cart with a logged-in customer’s previously saved items, leading to confusion and potential lost sales. In this post, we’ll explore a practical solution to clear the saved basket on login if the current session has products in it.

Understanding the Default Magento Behavior

By default, Magento stores the shopping cart data for guest users separately from those who are logged in as customers. When a user logs out or closes their browser, their guest cart is saved and can be retrieved on subsequent visits to the site via a cookie. However, if you log in with an existing customer account while having items in your guest basket, Magento will attempt to merge these two baskets during checkout.

The Desirable Behavior: Clearing the Saved Basket on Login

To provide a seamless shopping experience for your customers and avoid confusion, it is preferable to clear the saved guest cart when a user logs in. This ensures that only their current selection of products is maintained throughout the checkout process. Let’s dive into a step-by-step solution to achieve this desired behavior.

Implementing the Solution: Clearing the Saved Basket on Login

To implement this feature, we will need to create an observer that listens for the controller_action_predispatch_checkout_index_start event in Magento 2. This event is triggered before the checkout process begins, allowing us to clear the saved guest basket if necessary.

First, register your observer in the di.xml file of your custom module:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <event name="controller_action_predispatch_checkout_index_start">
        <observer name="clear_saved_basket_on_login" instance="Vendor\Module\Model\Observer\ClearSavedBasketOnLogin"/>
    </event>
</config>

Next, create the observer class Vendor\Module\Model\Observer\ClearSavedBasketOnLogin:

namespace Vendor\Module\Model\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\Quote;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Quote\Model\QuoteFactory;

class ClearSavedBasketOnLogin implements ObserverInterface
{
    protected $quoteFactory;
    protected $customerSession;

    public function __construct(
        QuoteFactory $quoteFactory,
        CustomerSession $customerSession
    ) {
        $this->quoteFactory = $quoteFactory;
        $this->customerSession = $customerSession;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        if ($this->customerSession->isLoggedIn() && $this->customerSession->getQuote()->hasItems()) {
            $savedQuoteId = $this->customerSession->getCustomer()->getSharedStoreId();
            $savedQuote = $this->quoteFactory->create()->load($savedQuoteId);

            if ($savedQuote && $savedQuote->getId()) {
                foreach ($savedQuote->getAllVisibleItems() as $item) {
                    $savedQuote->removeItem($item);
                }
                $savedQuote->save();
            }
        }

        return $this;
    }
}

This observer checks if a customer is logged in and has items in their current cart. If so, it retrieves the saved guest basket associated with the customer’s shared store ID and clears all visible items from that saved quote.

Testing the Solution

To test this feature effectively:

  1. Log out of your Magento backend.
  2. As a guest user, add some products to your cart.
  3. Log in as a registered customer without clearing your guest cart first.
  4. Initiate the checkout process.

Upon navigating to the checkout page, you should find only the items from your current logged-in session present in the basket, with the saved guest basket having been cleared automatically.

Conclusion

Clearing the saved Magento basket on login is a crucial step in enhancing the user experience and preventing confusion during the checkout process. By implementing the observer-based solution described above, you can ensure that only the relevant products are maintained for each customer session.

Remember to adapt the module name (Vendor\Module) according to your actual Magento setup. If you’re looking for more advanced customizations or need expert assistance with your e-commerce platform, feel free to reach out to our team at Laravel Company. We specialize in providing tailored solutions and consultancy services to help businesses thrive online.

With this implementation, you can now provide a seamless and intuitive shopping experience for your customers while minimizing the risk of lost sales due to basket merging issues. Happy coding!

Checkout Catalog Themes Customers

Our network