0

Suppose a guest has 10 orders with his email id xx@yy.com. 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?

1

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

<?php 
    require_once 'app/Mage.php';
    umask(0);
    Mage::init();

    $toCustomer = Mage::getModel('customer/customer')->loadByEmail('xx@yy.com');

    $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
        $orderbyid->setCustomerId($toCustomer->getId());
        $orderbyid->setCustomerFirstname($toCustomer->getFirstname());
        $orderbyid->setCustomerLastname($toCustomer->getLastname());
        $orderbyid->setCustomerEmail($toCustomer->getEmail());
        $orderbyid->save(); 
    }
?>

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 '15 at 10:47
  • their is no default feature – Pradeep Sanku Apr 2 '15 at 10:51
2

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.

<?php
// Initialize environment
require_once 'app/Mage.php';
umask(0);
Mage::init();

// 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
$collection->join(
    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
    $collection->join(
        array('store' => 'core/store'),
        'main_table.store_id = store.store_id',
        array()
    );

    // 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
$collection->setPageSize(100);

// 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
        $order->setCustomerId($order->getData('linked_customer_id'));
        $order->setCustomerIsGuest(false);

        // 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
        $order->save();

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

        // Increment the counter
        $count++;
    } 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, privacy policy and cookie policy

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