5

I want to link a guest order to a existing account in Magento. Does someone know which fields on which tables I have to change to accomplish this?

Is there maybe a easier way?

5 Answers 5

8

This also seem to work..

$order_id = 5;  // Your Order ID
$customer_id = 7;  //Customer ID

$_order = Mage::getModel('sales/order')->load($order_id);

if($_order->getCustomerId() == NULL){
    $_order->setCustomerId($customer_id);
    $_order->save();
}
7

If you are on Magento >= 1.4.1, you can run that in your database :

UPDATE sales_flat_order 
   SET customer_id = YOUR_CUSTOMER_ID_HERE 
WHERE entity_id = YOUR_ORDER_ID_HERE AND customer_id IS NULL

You will also need to upgrade the "grid" table :

UPDATE sales_flat_order_grid 
    SET customer_id = YOUR_CUSTOMER_ID_HERE 
WHERE entity_id = YOUR_ORDER_ID_HERE AND customer_id IS NULL
2
  • 1
    If you're on magento 2 this works as well, but the tables are called sales_order and sales_order_grid. Great tip!
    – helefa
    Jan 5, 2020 at 10:42
  • I just wanted to place a note to state that there is no customer_id column in sales_invoice or sales_invoice_grid tables, so no need to worry about those.
    – fearfelt
    Jan 15 at 11:54
3

Since from backend it's easier to get increment_id from order than entity id i prefer use this the following two queries:

UPDATE sales_flat_order 
  SET customer_id = YOUR_CUSTOMER_ID_HERE, customer_is_guest=0, customer_group_id = YOUR_CUSTOMER_GROUP_ID_HERE
  WHERE increment_id = YOUR_INCREMENT_ID_HERE AND customer_id IS NULL

UPDATE sales_flat_order_grid 
  SET customer_id = YOUR_CUSTOMER_ID_HERE, 
  WHERE increment_id = YOUR_INCREMENT_ID_HERE AND customer_id IS NULL
1

I am using magento 1.9.0.1 and I use to change following information:
For Table sales_flat_order:
=> change customer_id from NULL to new customer_id
=> customer_is_guest to 0.

Also for Table sales_flat_order_grid:
=> change customer_id from NULL to new customer_id

Following 2 queries will do the job:

UPDATE sales_flat_order 
  SET customer_id = (Your_Customer_ID), customer_is_guest=0 
  WHERE entity_id = Your_Order_ID_HERE AND customer_id IS NULL

UPDATE sales_flat_order_grid 
  SET customer_id = Your_Customer_ID 
  WHERE entity_id = Your_Order_ID AND customer_id IS NULL
1

To expand on what R.S posted:

If you want the admin order screen to reflect what group the member is in you need a few more lines:

$customer_id = xxxx;  //Customer ID
$order_increment_id = xxxxxxxxx; //Admin order increment id
$_order = Mage::getModel('sales/order')->loadByIncrementId($order_increment_id);

if($_order->getCustomerId() == NULL){
    $_order->setCustomerId($customer_id);
}

if($_order->getCustomerIsGuest() == "1"){
    $_order->setCustomerIsGuest(0);
}

if($_order->getCustomerGroupId() == "0"){
    $_order->setCustomerGroupId(1);
}

$_order->save();

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.