copy orders of a customer to other customer in magento
-
16-10-2019 - |
Question
Is it possible to fetch all the orders of a customer and copy into other customer in magento?
Solution
Two ways to handle this -
Edit the database directly. Don't do this.
Create a one-off script that will reassociate orders with other customers.
To do so, you can run something akin to:
$fromCustomer = $customer->loadByEmail('fromemail@host.net');
$toCustomer = $customer->loadByEmail('toemail@host.net');
$orderEmailCollection = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_email',$fromCustomer->getEmail());
$orderIdCollection = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_id',$fromCustomer->getId());
foreach($orderEmailCollection as $order){
$order->setCustomerId($toCustomer->getId());
$order->setCustomerEmail($toCustomer->getEmail());
$order->save();
}
foreach($orderIdCollection as $order){
$order->setCustomerId($toCustomer->getId());
$order->setCustomerEmail($toCustomer->getEmail());
$order->save();
}
OTHER TIPS
There's this script:
https://github.com/iateadonut/magento_copy_customer
It grabs a customer and all his orders through the single command:
$mg->copy_customer(1234);
if 1234 is the customer_entity.entity_id - You can take a look in the source code to see how the table restraints were queried to make sure all rows were grabbed.
There are other methods in there that should be able to move an order for you. You could fork it and give your work to others or ask the author to build something specific.