orderFactory = $orderFactory; $this->storeManager = $storeManager; $this->customerFactory = $customerFactory; $this->orderCustomerManager = $orderCustomerManager; $this->orderRepository = $orderRepository; $this->addressFactory = $addressFactory; $this->messageManager = $messageManager; } /** * Execute method for the Observer * * @param \Magento\Framework\Event\Observer $observer */ public function execute(Mag_Obs $observer) { // Get order Id. $orderIds = $observer->getEvent()->getOrderIds(); $orderId = $orderIds[0]; // Load the order with this ID into the order factory. $order = $this->orderFactory->create()->load($orderId); // Retrieve the billing and shipping addresses for this order. $billingAddress = $order->getBillingAddress(); $shippingAddress = $order->getShippingAddress(); // Create customer factory; load the customer with this order's email. $customer= $this->customerFactory->create(); $customer->setWebsiteId($this->storeManager->getStore()->getWebsiteId()); $customer->loadByEmail($order->getCustomerEmail()); // If the customer factory does not have a customer ID, they don't exist. $shippingSet = false; if ($order->getId() && !$customer->getId()) { $newCustomer = true; // Create a new customer for this order. // An instance of CustomerInterface will be returned. $customer = $this->orderCustomerManager->create($orderId); // Set the default billing address for this new customer. $address = $this->addressFactory->create(); $address->setData($billingAddress->getData()); $address->setCustomerId($customer->getId()); $address->setIsDefaultBilling("1"); // Check if the billing and shipping addresses match. // If they do, this will also be the shipping address. if ($billingAddress->getPostcode() == $shippingAddress->getPostcode() && ($billingAddress->getStreet[0] ?? "") == ($shippingAddress->getStreet[0] ?? "")) { $address->setIsDefaultShipping("1"); $shippingSet = true; } $address->setSaveInAddressBook("1"); $address->save(); $this->messageManager->addSuccessMessage(__("We have created an account so you can" . " log into this website, track orders, and place future orders more easily. We" . " have sent you an email with instructions to create a password and log in.")); } else { // If not, the customer already existed. Link the order to that customer. $newCustomer = false; $order->setCustomerId($customer->getId()); $order->setCustomerIsGuest(0); $this->orderRepository->save($order); // We don't add the billing address, since we should not change the default. $this->messageManager->addSuccessMessage(__("You have shopped at this website before." . " We have linked this order with your existing account for your convenience.")); } // Lastly, we add the shipping address to the customer. // Iff this was a new customer, we set this as the default. // If the shipping address matched billing, we can skip this. if (!$shippingSet) { $address = $this->addressFactory->create(); $address->setData($shippingAddress->getData()); $address->setCustomerId($customer->getId()); $address->setIsDefaultShipping($newCustomer ? "1" : "0"); $address->setSaveInAddressBook("1"); $address->save(); } } }