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
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
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.
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};
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)
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.
$orderId = YOURID;
$order = $this->orderRepository->get($orderId);
if ($order->getId() && !$order->getCustomerId()) {
$order->setCustomerId($customer->getId());
$order->setCustomerIsGuest(0);
$this->orderRepository->save($order);
}
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;