Question

I want to assign a quest order to a customer, using a custom script. I tried the following, do not get any error, but it is not assigned.

What am I missing?

CODE:

<?php 
ini_set('display_errors', 'On');
error_reporting(E_ALL); 

require('../app/Mage.php'); // this is assuming your script is located in the Magento root dir
Mage::app();

$toCustomer = Mage::getModel('customer/customer')->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail($customerEmail);
$orderbyid = Mage::getModel('sales/order')->loadByIncrementId('123456');

$orderbyid->setCustomerId($toCustomer->getId()); 
$orderbyid->setCustomerFirstname($toCustomer->getFirstname()); 
$orderbyid->setCustomerLastname($toCustomer->getLastname()); 
$orderbyid->setCustomerEmail($toCustomer->getEmail());
$orderbyid->save(); 
?>
Was it helpful?

Solution

Try the below code to assign customer to orders:

<?php 
ini_set('display_errors', 'On');
error_reporting(E_ALL); 

require('../app/Mage.php'); // this is assuming your script is located in the Magento root dir
Mage::app();

$toCustomer = Mage::getModel('customer/customer')->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail($customerEmail);
$orderbyid = Mage::getModel('sales/order')->loadByIncrementId('123456');

$orderbyid->setCustomerId($toCustomer->getId()); 
$orderbyid->setCustomerFirstname($toCustomer->getFirstname()); 
$orderbyid->setCustomerLastname($toCustomer->getLastname()); 
$orderbyid->setCustomerEmail($toCustomer->getEmail());
$orderbyid->setCustomerIsGuest(0); //Added by Sukumar
$orderbyid->save(); 
?>

Assume $orderbyid and $toCustomer->getId() getting correct values.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top