/** * 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; }
/** * {@inheritdoc} */ public function updateCustomerDetailsByEmail($customerEmail, CustomerDetails $customerDetails, $websiteId = null) { $customerData = $customerDetails->getCustomer(); $customerId = $this->getCustomerByEmail($customerEmail, $websiteId)->getId(); if ($customerData->getId() && $customerData->getId() !== $customerId) { throw new StateException('Altering the customer ID is not permitted'); } $customerData = $this->customerBuilder->populate($customerData)->setId($customerId)->create(); $customerDetails = $this->customerDetailsBuilder->populate($customerDetails)->setCustomer($customerData)->create(); return $this->updateCustomer($customerDetails); }
/** * {@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; }