Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssignOrderToCustomerObserver.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
8 namespace Magento\Sales\Observer;
9 
14 
19 {
23  private $orderRepository;
24 
28  public function __construct(OrderRepositoryInterface $orderRepository)
29  {
30  $this->orderRepository = $orderRepository;
31  }
32 
36  public function execute(Observer $observer)
37  {
38  $event = $observer->getEvent();
40  $customer = $event->getData('customer_data_object');
42  $delegateData = $event->getData('delegate_data');
43  if (array_key_exists('__sales_assign_order_id', $delegateData)) {
44  $orderId = $delegateData['__sales_assign_order_id'];
45  $order = $this->orderRepository->get($orderId);
46  if (!$order->getCustomerId()) {
47  //if customer ID wasn't already assigned then assigning.
48  $order->setCustomerId($customer->getId());
49  $order->setCustomerIsGuest(0);
50  $this->orderRepository->save($order);
51  }
52  }
53  }
54 }
$customer
Definition: customers.php:11
$orderRepository
Definition: order.php:69
$order
Definition: order.php:55