6

I've got some order from guest and later they create their account and ask to link the old orders.

How can I do it?

I tried to update the table sales_order but it wasn't enough to work.

Thanks

2
10

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 PHP (if customer exist)

public function __construct(
    .....
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
) {
    ...
    $this->orderRepository = $orderRepository;
    $this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

public function execute()
{
    $incrementId = {YOUR ORDER ID};
    $customerId = {YOUR CUSTOMER ID};

    $searchCriteria = $this->searchCriteriaBuilder->addFilter('increment_id', $incrementId, 'eq')->create();
    $order = $this->orderRepository->getList($searchCriteria)->getFirstItem();

     if ($order->getId() && !$order->getCustomerId()) 
     {
        $order->setCustomerId($customerId);
        $order->setCustomerIsGuest(0);
        $this->orderRepository->save($order);
     ...

Using a Module https://github.com/magepal/magento2-guest-to-customer

0
2

In addition to Renon Stewart answer, in case you have downloadable products in your orders you also need to attach the links to the customer. You can do so with the following SQL queries:

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};
UPDATE downloadable_link_purchased SET customer_id = {YOUR CUSTOMER ID} WHERE order_id = {YOUR ORDER ID};
0

You have to update both tables:

sales_order and sales_order_grid

and change the customer ID from NULL to the ID of your customer.

0
    $orderId = YOURID;

    $order = $this->orderRepository->get($orderId);

    if ($order->getId() && !$order->getCustomerId()) {
        $order->setCustomerId($customer->getId());
        $order->setCustomerIsGuest(0);
        $this->orderRepository->save($order);
    }
0

You can try this extension , It's free

https://marketplace.magento.com/mageacademy-mergecustomer.html

It really help you out.

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.