0

We have a few orders that were done by a customer, but we need to move these into another account (same store). What's the best way of doing this - can it be done by editing the database directly or through a script? We don't have root access to the server.

1

you can do this progrmmatically

<?php $customer = Mage::getModel('customer/customer'); ?>
<?php $fromCustomer = Mage::getModel('customer/customer')->loadByEmail($customer_b_emailid); ?>
<?php $toCustomer = Mage::getModel('customer/customer')->loadByEmail($customer_a_emailid); ?>

<?php $orderEmailCollection = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_email',$fromCustomer->getEmail()); ?>
<?php $orderIdCollection = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_id',$fromCustomer->getId()); ?>

<?php foreach($orderEmailCollection as $order): ?>
<?php $orderid = $order->getIncrementId(); ?>
<?php if($orderid == "200017139"): ?>
<?php $order->setCustomerId($toCustomer->getId()); ?>
<?php $order->setCustomerFirstname($toCustomer->getFirstname()); ?>
<?php $order->setCustomerLastname($toCustomer->getLastname()); ?>
<?php $order->setCustomerEmail($toCustomer->getEmail()); ?>
<?php $order->save(); ?>
<?php endif; ?>
<?php endforeach; ?>
1

you can edit the order entry from the sales order table from database, you will have to update the customer_id,names and email address, this will assign the order to new customer. But you may need to update all the address related entries to fully relate it to new customer otherwise the addresses will remain of old customer.

1

It is possible by editing the database directly (backup is recommended). The following gives you an idea of all the locations where customer_id is used

SELECT table_name, table_schema
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name = 'customer_id'
LIMIT 0 , 30    

+-------------------------------+-----------------+
| table_name                    | table_schema    |
+-------------------------------+-----------------+
| catalog_compare_item          | vanilla_1_7_0_2 |
| downloadable_link_purchased   | vanilla_1_7_0_2 |
| gift_message                  | vanilla_1_7_0_2 |
| log_customer                  | vanilla_1_7_0_2 |
| log_visitor_online            | vanilla_1_7_0_2 |
| newsletter_subscriber         | vanilla_1_7_0_2 |
| oauth_token                   | vanilla_1_7_0_2 |
| persistent_session            | vanilla_1_7_0_2 |
| poll_vote                     | vanilla_1_7_0_2 |
| product_alert_price           | vanilla_1_7_0_2 |
| product_alert_stock           | vanilla_1_7_0_2 |
| rating_option_vote            | vanilla_1_7_0_2 |
| report_compared_product_index | vanilla_1_7_0_2 |
| report_viewed_product_index   | vanilla_1_7_0_2 |
| review_detail                 | vanilla_1_7_0_2 |
| sales_billing_agreement       | vanilla_1_7_0_2 |
| sales_flat_invoice            | vanilla_1_7_0_2 |
| sales_flat_order              | vanilla_1_7_0_2 |
| sales_flat_order_address      | vanilla_1_7_0_2 |
| sales_flat_order_grid         | vanilla_1_7_0_2 |
| sales_flat_quote              | vanilla_1_7_0_2 |
| sales_flat_quote_address      | vanilla_1_7_0_2 |
| sales_flat_shipment           | vanilla_1_7_0_2 |
| sales_recurring_profile       | vanilla_1_7_0_2 |
| salesrule_coupon_usage        | vanilla_1_7_0_2 |
| salesrule_customer            | vanilla_1_7_0_2 |
| tag_relation                  | vanilla_1_7_0_2 |
| wishlist                      | vanilla_1_7_0_2 |
+-------------------------------+-----------------+

Out of this list you want to tackle those that have some order relation:

downloadable_link_purchased
gift_message
sales_flat_invoice 
sales_flat_order
sales_flat_order_address
sales_flat_order_grid 
sales_flat_quote 
sales_flat_quote_address
sales_flat_shipment 

these ones you might be able to skip (accounting for coupon usage) if you do not restrict coupons per customer/order

salesrule_customer 
salesrule_coupon_usage

this one you might be able to skip if you do not use recurring payments

sales_recurring_profile
1

I have created one module for this. However this needs to have same email id as while order was created.

public function relateOrder($id, $customer_id)
    {
        $customer = $this->_getCustomer($customer_id);
        $order = Mage::getModel('sales/order')->load($id);
        if($order->getId() == $id && $order->getCustomerIsGuest()){
            $order->setCustomerId($customer_id);
            $order->setCustomerIsGuest(0);
            $order->setCustomerGroupId($customer->getGroupId());
            $order->save();
        }
    }

 private function _getCustomer($customer_id)
    {
        return $customer = Mage::getModel('customer/customer')->load($customer_id);
    }

You can download the extension from here: http://www.magentocommerce.com/magento-connect/relate-customer-with-order.html

Hope this will help others.

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.