7

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

6 Answers 6

17

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: Example on my GitHub.

0
5

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

I know this is an old question, but since this is currently the first result in Google I would like to share a more robust way of handling this via PHP.

Magento offers a PHP service class that takes care of this process (\Magento\Sales\Model\Order\CustomerAssignment)

To utilize this you will need to retrieve the customer and order (eg. using the repositories) and then pass them to this service.

public function __construct(
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
    \Magento\Sales\Model\Order\CustomerAssignment $customerAssignment
) {
    $this->orderRepository = $orderRepository;
    $this->customerRepository = $customerRepository;
    $this->customerAssignment = $customerAssignment;
}

/**
 * @param int $orderId
 * @param int $customerId
 * @return void
 */
public function execute(int $orderId, int $customerId): void
{
    $order = $this->orderRepository->get($orderId);
    $customer = $this->customerRepository->getById($customerId);

    $this->customerAssignment->execute($order, $customer);
}

Alternatively, you could also retrieve the customer via email by using $this->customerRepository->get($customerEmail)

2
  • 1
    Hi Martie - this looks to be the best answer if it really is that simple; I just bought an extension from bsscommerce that claims that there are some reasons why their extension can't do this (their explanation is not clear), but maybe they are not aware of this functionality. What version has it been in, do you know? Commented Feb 29 at 20:47
  • Hi @grok_in_full, I’m not sure of the exact version, but according to the Magento 2 github repo the file was added 5 years ago, so this wasn’t originally available when this question was asked (in 2016)
    – Martie
    Commented Mar 1 at 21:51
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

in magepalExtension answer missed "and customer_group_id = 1" (General) for common cases So it should be if u havent specific cutomer_group for this customer

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

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.