/** * {@inheritdoc} */ public function create($orderId) { $order = $this->orderRepository->get($orderId); if ($order->getCustomerId()) { throw new AlreadyExistsException(__("This order already has associated customer account")); } $customerData = $this->objectCopyService->copyFieldsetToTarget('order_address', 'to_customer', $order->getBillingAddress(), []); $addresses = $order->getAddresses(); foreach ($addresses as $address) { $addressData = $this->objectCopyService->copyFieldsetToTarget('order_address', 'to_customer_address', $address, []); /** @var \Magento\Customer\Api\Data\AddressInterface $customerAddress */ $customerAddress = $this->addressFactory->create(['data' => $addressData]); if (is_string($address->getRegion())) { /** @var \Magento\Customer\Api\Data\RegionInterface $region */ $region = $this->regionFactory->create(); $region->setRegion($address->getRegion()); $region->setRegionCode($address->getRegionCode()); $region->setRegionId($address->getRegionId()); $customerAddress->setRegion($region); } $customerData['addresses'][] = $customerAddress; } /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */ $customer = $this->customerFactory->create(['data' => $customerData]); $account = $this->accountManagement->createAccount($customer); $order->setCustomerId($account->getId()); $this->orderRepository->save($order); return $account; }
/** * Add address to customer during create account * * @return AddressInterface|null */ protected function extractAddress() { if (!$this->getRequest()->getPost('create_address')) { return null; } $addressForm = $this->formFactory->create('customer_address', 'customer_register_address'); $allowedAttributes = $addressForm->getAllowedAttributes(); $addressData = []; $regionDataObject = $this->regionDataFactory->create(); foreach ($allowedAttributes as $attribute) { $attributeCode = $attribute->getAttributeCode(); $value = $this->getRequest()->getParam($attributeCode); if ($value === null) { continue; } switch ($attributeCode) { case 'region_id': $regionDataObject->setRegionId($value); break; case 'region': $regionDataObject->setRegion($value); break; default: $addressData[$attributeCode] = $value; } } $addressDataObject = $this->addressDataFactory->create(); $this->dataObjectHelper->populateWithArray($addressDataObject, $addressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface'); $addressDataObject->setRegion($regionDataObject); $addressDataObject->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false)); return $addressDataObject; }
/** * {@inheritdoc} */ public function run() { $this->logger->log('Installing customers:'); foreach ($this->fixtures as $file) { /** @var \Magento\SampleData\Helper\Csv\Reader $csvReader */ $fileName = $this->fixtureHelper->getPath($file); $csvReader = $this->csvReaderFactory->create(['fileName' => $fileName, 'mode' => 'r']); foreach ($csvReader as $row) { // Collect customer profile and addresses data $customerData['profile'] = $this->convertRowData($row, $this->getDefaultCustomerProfile()); if (!$this->accountManagement->isEmailAvailable($customerData['profile']['email'])) { continue; } $customerData['address'] = $this->convertRowData($row, $this->getDefaultCustomerAddress()); $customerData['address']['region_id'] = $this->getRegionId($customerData['address']); $address = $customerData['address']; $regionData = [RegionInterface::REGION_ID => $address['region_id'], RegionInterface::REGION => !empty($address['region']) ? $address['region'] : null, RegionInterface::REGION_CODE => !empty($address['region_code']) ? $address['region_code'] : null]; $region = $this->regionFactory->create(); $this->dataObjectHelper->populateWithArray($region, $regionData, '\\Magento\\Customer\\Api\\Data\\RegionInterface'); $addresses = $this->addressFactory->create(); unset($customerData['address']['region']); $this->dataObjectHelper->populateWithArray($addresses, $customerData['address'], '\\Magento\\Customer\\Api\\Data\\AddressInterface'); $addresses->setRegion($region)->setIsDefaultBilling(true)->setIsDefaultShipping(true); $customer = $this->customerFactory->create(); $this->dataObjectHelper->populateWithArray($customer, $customerData['profile'], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $customer->setAddresses([$addresses]); $this->accountManagement->createAccount($customer, $row['password']); $this->logger->logInline('.'); } } }
/** * {@inheritdoc} */ public function install($fixtures) { foreach ($fixtures as $fixture) { $filePath = $this->fixtureManager->getFixture($fixture); $rows = $this->csvReader->getData($filePath); $header = array_shift($rows); foreach ($rows as $row) { $data = []; foreach ($row as $key => $value) { $data[$header[$key]] = $value; } $row = $data; // Collect customer profile and addresses data $customerData['profile'] = $this->convertRowData($row, $this->getDefaultCustomerProfile()); if (!$this->accountManagement->isEmailAvailable($customerData['profile']['email'])) { continue; } $customerData['address'] = $this->convertRowData($row, $this->getDefaultCustomerAddress()); $customerData['address']['region_id'] = $this->getRegionId($customerData['address']); $address = $customerData['address']; $regionData = [RegionInterface::REGION_ID => $address['region_id'], RegionInterface::REGION => !empty($address['region']) ? $address['region'] : null, RegionInterface::REGION_CODE => !empty($address['region_code']) ? $address['region_code'] : null]; $region = $this->regionFactory->create(); $this->dataObjectHelper->populateWithArray($region, $regionData, '\\Magento\\Customer\\Api\\Data\\RegionInterface'); $addresses = $this->addressFactory->create(); unset($customerData['address']['region']); $this->dataObjectHelper->populateWithArray($addresses, $customerData['address'], '\\Magento\\Customer\\Api\\Data\\AddressInterface'); $addresses->setRegion($region)->setIsDefaultBilling(true)->setIsDefaultShipping(true); $customer = $this->customerFactory->create(); $this->dataObjectHelper->populateWithArray($customer, $customerData['profile'], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $customer->setAddresses([$addresses]); $this->appState->emulateAreaCode('frontend', [$this->accountManagement, 'createAccount'], [$customer, $row['password']]); } } }
/** * Get the original customer address. * * @return CustomerAddressInterface */ public function getOriginalCustomerAddress() { $address = $this->customerAddressFactory->create(); $region = $this->customerRegionFactory->create(); $addressData = $this->sessionManager->getOriginalCustomerAddressData(); if ($addressData) { $region->setRegionId($addressData['region']['region_id'])->setRegionCode($addressData['region']['region_id'])->setRegion($addressData['region']['region']); $address->setId($addressData['id'])->setCustomerId($addressData['customer_id'])->setCountryId($addressData['country_id'])->setStreet($addressData['street'])->setCompany($addressData['company'])->setTelephone($addressData['telephone'])->setFax($addressData['fax'])->setPostcode($addressData['postcode'])->setCity($addressData['city'])->setFirstname($addressData['firstname'])->setLastname($addressData['lastname'])->setMiddlename($addressData['middlename'])->setPrefix($addressData['prefix'])->setSuffix($addressData['suffix'])->setVatId($addressData['vat_id'])->setIsDefaultShipping($addressData['is_default_shipping'])->setIsDefaultBilling($addressData['is_default_billing'])->setRegion($region); } return $address; }
/** * Create address data object based on current address model. * * @param int|null $defaultBillingAddressId * @param int|null $defaultShippingAddressId * @return AddressInterface * @deprecated Use Api/Data/AddressInterface as a result of service operations. Don't rely on the model to provide * the instance of Api/Data/AddressInterface * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function getDataModel($defaultBillingAddressId = null, $defaultShippingAddressId = null) { $addressId = $this->getId(); $attributes = $this->metadataService->getAllAttributesMetadata(); $addressData = []; foreach ($attributes as $attribute) { $code = $attribute->getAttributeCode(); if ($this->getData($code) !== null) { if ($code === AddressInterface::STREET) { $addressData[$code] = $this->getDataUsingMethod($code); } else { $addressData[$code] = $this->getData($code); } } } /** @var RegionInterface $region */ $region = $this->regionDataFactory->create(); $region->setRegion($this->getRegion())->setRegionCode($this->getRegionCode())->setRegionId($this->getRegionId()); $addressData[AddressData::REGION] = $region; $addressDataObject = $this->addressDataFactory->create(); $this->dataObjectHelper->populateWithArray($addressDataObject, $addressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface'); if ($addressId) { $addressDataObject->setId($addressId); } if ($this->getCustomerId() || $this->getParentId()) { $customerId = $this->getCustomerId() ?: $this->getParentId(); $addressDataObject->setCustomerId($customerId); if ($defaultBillingAddressId == $addressId) { $addressDataObject->setIsDefaultBilling(true); } if ($defaultShippingAddressId == $addressId) { $addressDataObject->setIsDefaultShipping(true); } } return $addressDataObject; }
/** * @param array $taxAddress * @return \Magento\Customer\Api\Data\AddressInterface|null */ private function convertDefaultTaxAddress(array $taxAddress = null) { if (empty($taxAddress)) { return null; } /** @var \Magento\Customer\Api\Data\AddressInterface $addressDataObject */ $addressDataObject = $this->addressFactory->create()->setCountryId($taxAddress['country_id'])->setPostcode($taxAddress['postcode']); if (isset($taxAddress['region_id'])) { $addressDataObject->setRegion($this->regionFactory->create()->setRegionId($taxAddress['region_id'])); } return $addressDataObject; }
/** * Map quote address to customer address * * @param QuoteAddress $address * @return CustomerAddress */ public function mapAddress(QuoteAddress $address) { $customerAddress = $this->customerAddressFactory->create(); $customerAddress->setCountryId($address->getCountryId()); $customerAddress->setRegion($this->customerAddressRegionFactory->create()->setRegionId($address->getRegionId())); $customerAddress->setPostcode($address->getPostcode()); $customerAddress->setCity($address->getCity()); $customerAddress->setStreet($address->getStreet()); return $customerAddress; }