Let's say if a customer has an account, but for some reason can't log in, and creates an order by checking out as a guest. How can I assign this "guest order" to her customer account? This is so as she wants it in her order history. Any way to solve this? Appreciate the help!
-
Is it possible to do it for all the orders at once? All my customers were deleted, and I imported them back by CSV, but now the orders are no longer linked to them. Basically , orders and customers share the same email.– Dali AbassiCommented Jul 12, 2018 at 5:00
-
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review– Rama Chandran MCommented Jul 12, 2018 at 5:16
Add a comment
|
4 Answers
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'; ?>
<?php umask(0); ?>
<?php Mage::init(); ?>
<?php $toCustomer = Mage::getModel('customer/customer')->loadByEmail($customeremailid); ?>
<?php $orderbyid = Mage::getModel('sales/order')->loadByIncrementId('10000001'); // 10000001 is just an example replace with yours ?>
<?php $orderbyid->setCustomerId($toCustomer->getId()); ?>
<?php $orderbyid->setCustomerFirstname($toCustomer->getFirstname()); ?>
<?php $orderbyid->setCustomerLastname($toCustomer->getLastname()); ?>
<?php $orderbyid->setCustomerEmail($toCustomer->getEmail()); ?>
<?php $orderbyid->save(); ?>
Look for the customer id and the order id (not the increment id) and then run these queries on your database.
UPDATE sales_flat_order SET customer_id = {YOUR CUSTOMER ID} where entity_id = {YOUR ORDER ID};
UPDATE sales_flat_order_grid SET customer_id = {YOUR CUSTOMER ID} where entity_id = {YOUR ORDER ID};
For Magento 2
include('app/bootstrap.php');
use Magento\Framework\App\Bootstrap;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Model\AccountManagement;
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$email = '[email protected]'; // Your Customer Id
$incrementId = '000000047'; // Your Order Increment Id from Admin Listing
$toCustomer = $objectManager->create('Magento\Customer\Model\Customer')->setWebsiteId(1)->loadByEmail($email);
$orderbyid = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($incrementId);
$orderbyid->setCustomerId($toCustomer->getId());
$orderbyid->setCustomerFirstname($toCustomer->getFirstname());
$orderbyid->setCustomerLastname($toCustomer->getLastname());
$orderbyid->setCustomerEmail($toCustomer->getEmail());
$orderbyid->setCustomerIsGuest(0);
$orderbyid->save();
echo "Done";
Magento 2 without using an object manager.
$orderId = YOURID;
$order = $this->orderRepository->get($orderId);
if ($order->getId() && !$order->getCustomerId()) {
$order->setCustomerId($customer->getId());
$order->setCustomerIsGuest(0);
$this->orderRepository->save($order);
}