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
Magento2 – Transfer Guest Order to Customer Account Programmatically
customerguestguest-checkoutmagento2register
Related Solutions
According magento,there are 3 sales table where magento is save customer related
sales_flat_order
sales_flat_order_address
sales_flat_order_grid
sales_flat_order table
customer_id: here customer id save
.for guest user
it save as NULL
.You need to update this column by customer id
.
customer_is_guest: it save as 1 when guest
is saved an order and it value should be 0 when customer is register
.
customer_group_id: Need update customer group by customer group id .
sales_flat_order_grid table
customer_id: here customer id save
.for guest user
it save as NULL
.You need to update this column by customer id
.
sales_flat_order_address
customer_address_id: for guest user
it value is Null. You need update by customer address id whenever try transfer order
customer_id: here customer id save
.for guest user
it save as NULL
.You need to update this column by customer id
.
Code like:
$CustomerId=5;
$customerAddId=20;
$custommergrop=3;
$orderIncrementId=$this->getLastOrderId();
$orderObject=Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$billingId = $orderObject->getBillingAddress()->getId();
$shipping = $orderObject->isVirtual() ? null : $orderObject->getShippingAddress();
$billingObjct=Mage::getModel('sales/order_address')->load($billingId);
$billingObjct->setData('customer_id',$CustomerId);
$billingObjct->setData('customer_address_id',$customerAddId);
$billingObjct->save();
if($shipping){
$shippingAddressid=$orderObject->getShippingAddress()->getId();
$shippingAddressObject=Mage::getModel('sales/order_address')->load($shippingAddressid);
$shippingAddressObject->setData('customer_id',$CustomerId);
$shippingAddressObject->setData('customer_address_id',$customerAddId);
$shippingAddressObject->save();
}
$orderObject->setData('customer_id',$CustomerId);
$orderObject->setData('customer_is_guest',NULL); /*if null not work then try 0 instead of NULL*/
$orderObject->setData('customer_group_id',$custommergrop);
$orderObject->save();
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
Best Answer
Using SQL -
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.