How to assign customer to a quote?

How to assign customer to a quote?

September 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to assign customer to a quote?

Assigning Customers to Quotes in Magento: A Comprehensive Guide

As an experienced Magento developer, I often encounter scenarios where customers need to be assigned to existing quotes after they log in or create an account. This is particularly relevant when users add products to a cart before logging in.

Understanding the Customer Assignment Process

When a user adds items to their cart without being logged in, Magento creates a guest quote with a unique quote ID. However, if that same user then logs in or creates an account, Magento typically creates a new customer quote instead of assigning them to the existing (guest) quote.

The key to successfully assigning a customer to an existing quote lies in ensuring that Magento recognizes the continuity between the guest session and the logged-in customer’s session.

Incorrect Approach: Attempting Direct Assignment

In your provided code snippet, you’re attempting to directly assign the customer to the quote using the assignCustomer method:

$customer = Mage::getModel('customer/customer');
$customer->loadByEmail($email);
$quote          =    Mage::getModel('sales/quote')->load($quoteId);
$quote->assignCustomer($customer);

While this looks logical, it doesn’t work because Magento’s default behavior is to create a new quote for authenticated customers. The assignCustomer method simply updates the customer ID associated with an existing quote; it does not transfer the contents of one quote to another.

The Correct Approach: Merging Quotes

To successfully assign a logged-in customer to their previous guest quote, you need to merge the two quotes together.

First, you should retrieve both the guest and customer quotes:

$guestQuote = Mage::getModel('sales/quote')->load($guestQuoteId);
$customerQuote = Mage::getModel('sales/quote')->load($customerQuoteId);

Next, use the merge method to combine the items from the guest quote into the customer quote:

$customerQuote->setIsActive(false)
             ->save();

foreach ($guestQuote->getAllItems() as $item) {
    $newItem = clone $item;
    $customerQuote->addItem($newItem);
}

$customerQuote->collectTotals();
$customerQuote->save();

This code sets the customer quote to inactive, then iterates through each item in the guest quote. It clones each item and adds it to the customer quote using the addItem method.

Finally, you should remember to collect totals after merging items to ensure the new quote’s pricing is up-to-date:

$customerQuote->collectTotals();

Ensuring Quote Continuity

To prevent Magento from creating a new quote when a user logs in and has an existing guest quote, you can override the Mage_Checkout_Model_Session class.

In your custom module’s config.xml, add this configuration:

<config>
    <global>
        <models>
            <checkout>
                <rewrite>
                    <session>Your_Module_Model_Checkout_Session</session>
                </rewrite>
            </checkout>
        </models>
    </global>
</config>

Then, in your Session.php file for the checkout model, you can override the loadCustomerQuote method to check if a guest quote exists before creating a new customer quote:

public function loadCustomerQuote($customer) {
    $quote = Mage::getModel('sales/quote')->getCollection()
        ->addFieldToFilter('is_active', 1)
        ->addFieldToFilter('customer_id', $customer->getId())
        ->setPageSize(1)
        ->getFirstItem();

    if (!$quote) {
        // No active customer quote found, create a new one
        return parent::loadCustomerQuote($customer);
    }

    return $quote;
}

This ensures that Magento will merge the guest quote with the customer’s existing quotes instead of creating a new one.

Conclusion

Assigning customers to their previous quotes after they log in requires a deep understanding of how Magento handles quote management. By merging the guest and customer quotes together, you can provide a seamless shopping experience for your users.

Remember to test this functionality thoroughly, especially during checkout and order processing stages, to ensure all data is transferred correctly.

For more advanced customizations like this, I recommend referring to our detailed tutorials at Laravel Company.

Checkout Catalog Email Customers

Our network