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.