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 Engström
  • 413
  • 5
  • 6
jack
  • 920
  • 4
  • 15
  • 35

1 Answers1

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;
}
sduif
  • 955
  • 5
  • 13
  • 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 01 '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 01 '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: https://magento.stackexchange.com/questions/203535/magento-2-add-enable-disable-field-for-custom-module – sduif Feb 01 '18 at 11:30