2

I need to assign a Guest Order to a Customer, so i found this link

How to assign a Guest Order to a Customer - Magento 2

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

From the above two solutions For 1st solution (SQL)

  1. Where should I write SQL query so that it updates the tables sales_order and sales_order_grid before my myaccount phtml loads so that guest orders will be displayed.
  2. How can I get ORDER ID for guest checkout order as I need to update the sales_order table for guest order entity id.

    I tried this code

     public  function  getOrderId() {
      $order = $this->_checkoutSession->getLastRealOrder();
      $orderId=$order->getEntityId();
      return $orderId;
    }
    

    But this doesnt give any order id. Altough I am able to get customer id.

For 2nd solution (PHP)

  1. I am not able to get order id (entity_id).

How can I resolve these issues.

3
  • Check this: magento.stackexchange.com/a/232032/35758 – Prince Patel Feb 15 '19 at 6:52
  • @PrincePatel This will create an account on order success or after order sucess we generate account by creating password for that guest email id ? After that when account is created the previous order which has been guest order is not displayed under my account – summu Feb 15 '19 at 7:35
  • I tried this solution didn't work for me. The result is null . In guest account in fact there is no costommer id. Did you found any solution ? – navid torabazari Jan 7 at 14:39
1

With your PHP variant you can get the order Id, you just have to replace $order->getEntityId(); with $order->getId(); which is the correct method to get the entity id of the order.

This code should work for you:

public  function  getOrderId() {
  $orderIncrementId = $this->_checkoutSession->getLastRealOrderId();
  //load the order by Increment Id depending on the context of your script, for example like this
  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($orderIncrementId);
  $orderId=$order->getId();
  return $orderId;
}

It may also be possible that your order entity is already stored in session:

    $this->_checkoutSession->getLastOrderId();`
7
  • Still cant get order id for guest checkout order – summu Feb 15 '19 at 9:09
  • Sorry I have overseen that you are not loading the order in your code. I have updated my answer. – HelgeB Feb 15 '19 at 9:21
  • getting this error . Exception #0 (Exception): Warning: PDO::quote() expects parameter 1 to be string, object given in /Users/suman/the-webster/vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php on line 296 – summu Feb 15 '19 at 9:44
  • Ok updated to get the incrementId of the last order. Please check also the last code fraction I'be added, maybe that works too – HelgeB Feb 15 '19 at 9:57
  • still order id is null – summu Feb 15 '19 at 10:05

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.