/** * {@inheritdoc} */ public function createCustomerWithPasswordHash(\Magento\Customer\Service\V1\Data\CustomerDetails $customerDetails, $hash, $redirectUrl = '') { $customer = $customerDetails->getCustomer(); // This logic allows an existing customer to be added to a different store. No new account is created. // The plan is to move this logic into a new method called something like 'registerAccountWithStore' if ($customer->getId()) { $customerModel = $this->customerRegistry->retrieve($customer->getId()); $websiteId = $customerModel->getWebsiteId(); if ($this->isCustomerInStore($websiteId, $customer->getStoreId())) { throw new InputException('Customer already exists in this store.'); } // Reuse existing password $hash = $this->converter->getCustomerModel($customer->getId())->getPasswordHash(); } // Make sure we have a storeId to associate this customer with. if (!$customer->getStoreId()) { if ($customer->getWebsiteId()) { $storeId = $this->storeManager->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId(); } else { $storeId = $this->storeManager->getStore()->getId(); } $customer = $this->customerBuilder->populate($customer)->setStoreId($storeId)->create(); } try { $customerId = $this->saveCustomer($customer, $hash); } catch (\Magento\Customer\Exception $e) { if ($e->getCode() === CustomerModel::EXCEPTION_EMAIL_EXISTS) { throw new InputMismatchException('Customer with the same email already exists in associated website.'); } throw $e; } $this->customerAddressService->saveAddresses($customerId, $customerDetails->getAddresses()); $customerModel = $this->customerRegistry->retrieve($customerId); $newLinkToken = $this->mathRandom->getUniqueHash(); $customerModel->changeResetPasswordLinkToken($newLinkToken); $this->_sendEmailConfirmation($customerModel, $customer, $redirectUrl); return $this->converter->createCustomerFromModel($customerModel); }
/** * {@inheritdoc} */ public function updateCustomer(Data\CustomerDetails $customerDetails) { $customer = $customerDetails->getCustomer(); // Making this call first will ensure the customer already exists. $this->customerRegistry->retrieve($customer->getId()); $this->saveCustomer($customer, $this->converter->getCustomerModel($customer->getId())->getPasswordHash()); $addresses = $customerDetails->getAddresses(); // If $address is null, no changes must made to the list of addresses // be careful $addresses != null would be true of $addresses is an empty array if ($addresses !== null) { $existingAddresses = $this->customerAddressService->getAddresses($customer->getId()); /** @var Data\Address[] $deletedAddresses */ $deletedAddresses = array_udiff($existingAddresses, $addresses, function (Data\Address $existing, Data\Address $replacement) { return $existing->getId() - $replacement->getId(); }); // If $addresses is an empty array, all addresses are removed. // array_udiff would return the entire $existing array foreach ($deletedAddresses as $address) { $this->customerAddressService->deleteAddress($address->getId()); } $this->customerAddressService->saveAddresses($customer->getId(), $addresses); } return true; }
/** * Creates a collection item that represents a customer for the customer Grid. * * @param CustomerDetails $customerDetail Input data for creating the item. * @return \Magento\Framework\Object Collection item that represents a customer */ protected function createCustomerDetailItem(CustomerDetails $customerDetail) { $customer = $customerDetail->getCustomer(); $customerNameParts = array($customer->getPrefix(), $customer->getFirstname(), $customer->getMiddlename(), $customer->getLastname(), $customer->getSuffix()); $customerItem = new \Magento\Framework\Object(); $customerItem->setId($customer->getId()); $customerItem->setEntityId($customer->getId()); // All parts of the customer name must be displayed in the name column of the grid $customerItem->setName(implode(' ', array_filter($customerNameParts))); $customerItem->setEmail($customer->getEmail()); $customerItem->setWebsiteId($customer->getWebsiteId()); $customerItem->setCreatedAt($customer->getCreatedAt()); $customerItem->setGroupId($customer->getGroupId()); $billingAddress = null; foreach ($customerDetail->getAddresses() as $address) { if ($address->isDefaultBilling()) { $billingAddress = $address; break; } } if ($billingAddress !== null) { $customerItem->setBillingTelephone($billingAddress->getTelephone()); $customerItem->setBillingPostcode($billingAddress->getPostcode()); $customerItem->setBillingCountryId($billingAddress->getCountryId()); $region = is_null($billingAddress->getRegion()) ? '' : $billingAddress->getRegion()->getRegion(); $customerItem->setBillingRegion($region); } return $customerItem; }