0

I've got some orders from the guest user and later they create their account and ask to link the old orders. How can I Do it? Please Help

3 Answers 3

1

Using SQL -

UPDATE sales_order SET customer_id = {YOUR CUSTOMER ID}, customer_is_guest = 0 where entity_id = {YOUR ORDER ID};
UPDATE sales_order_grid SET customer_id = {YOUR CUSTOMER ID} where entity_id = {YOUR ORDER ID};

Using Extension -

Have a look at this solution - https://github.com/magepal/magento2-guest-to-customer

You can get some idea and customize it as per yours need.

1
  • Without using an extenstion Commented Nov 2, 2021 at 5:31
1

Obtain the customer and the order from repositories and then:

$order->setCustomerId($customer->getId());
$order->setCustomerIsGuest(0);
$order->setCustomerFirstname($customer->getFirstname());
$order->setCustomerLastname($customer->getLastname());

$this->orderRepository->save($order);
0

Finally, I got the solution for the above Question For Magento 2 Programatically!

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

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.