Suppose a guest has 10 orders with his email id [email protected]. Later he registered as a customer. Does Magento automatically convert his old order to his new account, or do we need to do this manually?


2 Answers 2


You have write a script and excute it.

You can create a file called assign_order.php with the following code and upload at your magento root directory and run this file with the www.domain.com/assign_order.php

    require_once 'app/Mage.php';

    $toCustomer = Mage::getModel('customer/customer')->loadByEmail('[email protected]');

    $orders_inc_ids = array('10000001','10000002','10000003','10000004','10000005');

    foreach($orders_inc_ids as $order_inc_id){
        $orderbyid = Mage::getModel('sales/order')->loadByIncrementId($order_inc_id); // here $order_inc_id contains ur order's increment id

for more information on the how to assign guest orders to customers refer my blog http://pradeepsanku.com/2014/04/30/assign-magento-order-to-customer/

  • so you are saying , magento doesn't do that automatically right !
    – Sree
    Apr 2, 2015 at 10:47
  • their is no default feature Apr 2, 2015 at 10:51

First, linking just by email has pitfalls. You could link order to customers mistakenly and cause issues. That aside, this is a generic linking script that would link all guest orders to customer accounts.

// Initialize environment
require_once 'app/Mage.php';

// Get the order collection resource
/** @var Mage_Sales_Model_Resource_Order_Collection $collection */
$collection = Mage::getModel('sales/order')->getCollection();

// Filter to orders without a customer
$collection->addFieldToFilter('customer_id', array('null' => true));

// Inner-join to customer table via email
    array('customer' => 'customer/entity'),
    'main_table.customer_email = customer.email',
    array('linked_customer_id' => 'entity_id')

// Limit to same website if configured that way
/** @var Mage_Customer_Model_Config_Share $sharingConfig */
$sharingConfig = Mage::getSingleton('customer/config_share');
if ($sharingConfig->isWebsiteScope()) {
    // Give some feedback about the process
    echo "Limiting linking to website scope.\n";

    // Inner-Join orders to stores to get access to the website_id
        array('store' => 'core/store'),
        'main_table.store_id = store.store_id',

    // Filter customers by using joined website_id as the limiter
    $collection->addFieldToFilter('store.website_id = customer.website_id');

// Limit processing run size to 100 records

// Track the number of linked/updated records
$count = 0;

// Give some feedback about the process
echo sprintf("Attempting to link %d orders.\n\n", $collection->count());

// Loop over all the candidate orders
foreach ($collection as $order) {
    /** @var Mage_Sales_Model_Order $order */
    // Give some feedback about the process
    echo sprintf('Trying to link order (%s) to customer (%s)... ', $order->getIncrementId(), $order->getCustomerEmail());
    try {
        // Change the order to being linked to a customer record

        // Add a comment on the order to record that the order was linked to a customer
        $order->addStatusHistoryComment('Automatically linking order to customer by email address');

        // Save changes

        // Give some feedback about the process
        echo sprintf("Succeeded.\n");

        // Increment the counter
    } catch (Exception $e) {
        // Give some feedback about the process
        echo sprintf("Failed! '%s'\n", $e->getMessage());

// Give some feedback about the process
echo sprintf("Linked %d orders.\n\n", $count);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.