1

I am trying to assign a guest order to a specific customer programmatically.

I have loaded the collection of orders placed by the guest users using following code:

public function __construct(
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory    $orderCollectionFactory,
) {
    $this->orderCollectionFactory = $orderCollectionFactory;
}

public function getGuestOrderCollection()
{
    $orderCollecion = $this->orderCollectionFactory
        ->create()
        ->addFieldToSelect('*');

    $orderCollecion->addAttributeToFilter('customer_is_guest', ['eq'=>1]);
    foreach($orderCollecion as $orderData){
        if($orderData->getCustomerId()){
            $order->setCustomerId($orderData->getCustomerId());
            $order->setCustomerIsGuest(0);
            $this->orderRepository->save($orderData);
            $message = "DONE";
        } else {
            $message = "Fail";
        }
    }
    return $message;
}

It doesn't work. It's not saving the order to that customer.

Not getting any error though.

What am I doing wrong? Any help would be greatly appreciated.

1
  • @Nisse i8t would be better if you can answer it.
    – jack
    Feb 1, 2018 at 5:29

1 Answer 1

3

Note that your code doesn't make a lot of sense to me. An order that was saved as a guest order should not have a customer_id set. Is your code passing into the if($orderData->getCustomerId()) block at all?

Also, do note that your returned $message variable will currently only be set for the last order in the collection.

That said, it looks like you are trying to save $order, while you should actually be using $orderData, like so:

public function __construct(
  \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
) {
  $this->orderCollectionFactory = $orderCollectionFactory;
}

public function getGuestOrderCollection() {

  $orderCollecion = $this->orderCollectionFactory
    ->create()
    ->addFieldToSelect('*');

  $orderCollecion->addAttributeToFilter('customer_is_guest', ['eq'=>1]);
  foreach($orderCollecion as $orderData){

    if($orderData->getCustomerId()) {
      $orderData->setCustomerId($orderData->getCustomerId());
      $orderData->setCustomerIsGuest(0);
      $orderData->save($orderData);
      $message = "DONE";
    } else{  
      $message = "Fail";
    }

  return $message;
}
4
  • Hi, you can say i want to update the customer email field in order. Is there any way to do this? will it work with cron? I am trying this in cron.
    – jack
    Jan 31, 2018 at 12:46
  • You can update the customer email field with $orderData->setCustomerEmail('[email protected]');
    – sduif
    Feb 1, 2018 at 10:29
  • Yes thanks working. Is it possible to manage cron through configuration? i mean if module is disable then our cron also stay disabled?
    – jack
    Feb 1, 2018 at 11:22
  • 1
    Only if you disable the module entirely through app/etc/config.php. Otherwise you could opt for creating a custom setting with enable/disable in your module. See this question for an example: magento.stackexchange.com/questions/203535/…
    – sduif
    Feb 1, 2018 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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