5

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!

2
  • 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. Commented 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 Commented Jul 12, 2018 at 5:16

4 Answers 4

6

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(); ?>
0
8

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};
0
0

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";
0

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);
    }

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.