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.

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

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;
}
  • 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 '18 at 12:46
  • You can update the customer email field with $orderData->setCustomerEmail('yourcustomeremail@example.com'); – sduif Feb 1 '18 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 '18 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 '18 at 11:30

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.