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