/** * @param \Magento\Quote\Model\Quote $quote * @return \Magento\Quote\Model\Quote */ public function prepareQuoteForNewCustomer(\Magento\Quote\Model\Quote $quote) { $billing = $quote->getBillingAddress(); $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress(); $billing->setDefaultBilling(true); if ($shipping && !$shipping->getSameAsBilling()) { $shipping->setDefaultShipping(true); $address = $shipping->exportCustomerAddress(); $shipping->setCustomerAddressData($address); } elseif ($shipping) { $billing->setDefaultShipping(true); } $address = $shipping->exportCustomerAddress(); $billing->setCustomerAddressData($address); foreach (['customer_dob', 'customer_taxvat', 'customer_gender'] as $attribute) { if ($quote->getData($attribute) && !$billing->getData($attribute)) { $billing->setData($attribute, $quote->getData($attribute)); } } $customer = $this->customerFactory->create(); $this->dataObjectHelper->populateWithArray($customer, $this->copyObject->getDataFromFieldset('checkout_onepage_billing', 'to_customer', $billing), '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $customer->setEmail($quote->getCustomerEmail()); $customer->setPrefix($quote->getCustomerPrefix()); $customer->setFirstname($quote->getCustomerFirstname()); $customer->setMiddlename($quote->getCustomerMiddlename()); $customer->setLastname($quote->getCustomerLastname()); $customer->setSuffix($quote->getCustomerSuffix()); $quote->setCustomer($customer); $quote->addCustomerAddress($billing->exportCustomerAddress()); if ($shipping->hasCustomerAddress()) { $quote->addCustomerAddress($shipping->exportCustomerAddress()); } return $quote; }
/** * {@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; }
/** * {@inheritdoc} */ public function delete($id) { /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */ $customer = $this->customerDataFactory->create(); $customer = $this->entityManager->load($customer, $id); try { $this->entityManager->delete($customer); } catch (\Exception $e) { return false; } return true; }
public function testUpdateDataOverrideExistingData() { /** @var \Magento\Customer\Model\Data\Customer $customerData */ $customerData = $this->customerFactory->create()->setId(2)->setFirstname('John')->setLastname('Doe')->setDefaultBilling(1); $this->customerModel->updateData($customerData); /** @var \Magento\Customer\Model\Data\Customer $updatedCustomerData */ $updatedCustomerData = $this->customerFactory->create()->setId(3)->setFirstname('Jane')->setLastname('Smith')->setDefaultBilling(0); $updatedCustomerData = $this->customerModel->updateData($updatedCustomerData)->getDataModel(); $this->assertEquals(3, $updatedCustomerData->getId()); $this->assertEquals('Jane', $updatedCustomerData->getFirstname()); $this->assertEquals('Smith', $updatedCustomerData->getLastname()); $this->assertEquals(0, $updatedCustomerData->getDefaultBilling()); }
/** * Retrieve Customer instance * * @return \Magento\Customer\Api\Data\CustomerInterface */ public function getCustomer() { if ($this->_customer === null) { $params = $this->urlDecoder->decode($this->_getRequest()->getParam('data')); $data = explode(',', $params); $customerId = abs(intval($data[0])); if ($customerId && $customerId == $this->_customerSession->getCustomerId()) { $this->_customer = $this->_customerRepository->getById($customerId); } else { $this->_customer = $this->_customerFactory->create(); } } return $this->_customer; }
/** * Create customer using setters. * * @param array $additional * @return CustomerInterface */ public function createSampleCustomerDataObject(array $additional = []) { $customerAddress1 = $this->customerAddressFactory->create(); $customerAddress1->setCountryId('US'); $customerAddress1->setIsDefaultBilling(true); $customerAddress1->setIsDefaultShipping(true); $customerAddress1->setPostcode('75477'); $customerAddress1->setRegion(Bootstrap::getObjectManager()->create('Magento\\Customer\\Api\\Data\\RegionInterfaceFactory')->create()->setRegionCode(self::ADDRESS_REGION_CODE1)->setRegion('Alabama')->setRegionId(1)); $customerAddress1->setStreet(['Green str, 67']); $customerAddress1->setTelephone('3468676'); $customerAddress1->setCity(self::ADDRESS_CITY1); $customerAddress1->setFirstname('John'); $customerAddress1->setLastname('Smith'); $address1 = $this->dataObjectProcessor->buildOutputDataArray($customerAddress1, 'Magento\\Customer\\Api\\Data\\AddressInterface'); $customerAddress2 = $this->customerAddressFactory->create(); $customerAddress2->setCountryId('US'); $customerAddress2->setIsDefaultBilling(false); $customerAddress2->setIsDefaultShipping(false); $customerAddress2->setPostcode('47676'); $customerAddress2->setRegion(Bootstrap::getObjectManager()->create('Magento\\Customer\\Api\\Data\\RegionInterfaceFactory')->create()->setRegionCode(self::ADDRESS_REGION_CODE2)->setRegion('Alabama')->setRegionId(1)); $customerAddress2->setStreet(['Black str, 48', 'Building D']); $customerAddress2->setTelephone('3234676'); $customerAddress2->setCity(self::ADDRESS_CITY2); $customerAddress2->setFirstname('John'); $customerAddress2->setLastname('Smith'); $address2 = $this->dataObjectProcessor->buildOutputDataArray($customerAddress2, 'Magento\\Customer\\Api\\Data\\AddressInterface'); $customerData = $this->getCustomerSampleData(array_merge([CustomerData::KEY_ADDRESSES => [$address1, $address2]], $additional)); $customer = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($customer, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); return $customer; }
/** * {@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']]); } } }
/** * Create customer using setters. * * @return CustomerInterface */ public function createSampleCustomerDataObject() { $customerAddress1 = $this->customerAddressFactory->create(); $customerAddress1->setCountryId('US'); $customerAddress1->setIsDefaultBilling(true); $customerAddress1->setIsDefaultShipping(true); $customerAddress1->setPostcode('75477'); $customerAddress1->setRegion(Bootstrap::getObjectManager()->create('Magento\\Customer\\Api\\Data\\RegionInterfaceFactory')->create()->setRegionCode(self::ADDRESS_REGION_CODE1)->setRegion('Alabama')->setRegionId(1)); $customerAddress1->setStreet(['Green str, 67']); $customerAddress1->setTelephone('3468676'); $customerAddress1->setCity(self::ADDRESS_CITY1); $customerAddress1->setFirstname('John'); $customerAddress1->setLastname('Smith'); $address1 = $this->dataObjectProcessor->buildOutputDataArray($customerAddress1, 'Magento\\Customer\\Api\\Data\\AddressInterface'); $customerAddress2 = $this->customerAddressFactory->create(); $customerAddress2->setCountryId('US'); $customerAddress2->setIsDefaultBilling(false); $customerAddress2->setIsDefaultShipping(false); $customerAddress2->setPostcode('47676'); $customerAddress2->setRegion(Bootstrap::getObjectManager()->create('Magento\\Customer\\Api\\Data\\RegionInterfaceFactory')->create()->setRegionCode(self::ADDRESS_REGION_CODE2)->setRegion('Alabama')->setRegionId(1)); $customerAddress2->setStreet(['Black str, 48', 'Building D']); $customerAddress2->setTelephone('3234676'); $customerAddress2->setCity(self::ADDRESS_CITY2); $customerAddress2->setFirstname('John'); $customerAddress2->setLastname('Smith'); $address2 = $this->dataObjectProcessor->buildOutputDataArray($customerAddress2, 'Magento\\Customer\\Api\\Data\\AddressInterface'); $customerData = [CustomerData::FIRSTNAME => self::FIRSTNAME, CustomerData::LASTNAME => self::LASTNAME, CustomerData::EMAIL => 'janedoe' . uniqid() . '@example.com', CustomerData::CONFIRMATION => self::CONFIRMATION, CustomerData::CREATED_AT => self::CREATED_AT, CustomerData::CREATED_IN => self::STORE_NAME, CustomerData::DOB => self::DOB, CustomerData::GENDER => self::GENDER, CustomerData::GROUP_ID => self::GROUP_ID, CustomerData::MIDDLENAME => self::MIDDLENAME, CustomerData::PREFIX => self::PREFIX, CustomerData::STORE_ID => self::STORE_ID, CustomerData::SUFFIX => self::SUFFIX, CustomerData::TAXVAT => self::TAXVAT, CustomerData::WEBSITE_ID => self::WEBSITE_ID, CustomerData::KEY_ADDRESSES => [$address1, $address2], 'custom_attributes' => [['attribute_code' => 'disable_auto_group_change', 'value' => '0']]]; $customer = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($customer, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); return $customer; }
public function testUpdateCustomerException() { $customerData = $this->_createCustomer(); $existingCustomerDataObject = $this->_getCustomerData($customerData[Customer::ID]); $lastName = $existingCustomerDataObject->getLastname(); //Set non-existent id = -1 $customerData[Customer::LASTNAME] = $lastName . 'Updated'; $customerData[Customer::ID] = -1; $newCustomerDataObject = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($newCustomerDataObject, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $serviceInfo = ['rest' => ['resourcePath' => self::RESOURCE_PATH . "/-1", 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT], 'soap' => ['service' => self::SERVICE_NAME, 'serviceVersion' => self::SERVICE_VERSION, 'operation' => self::SERVICE_NAME . 'Save']]; $newCustomerDataObject = $this->dataObjectProcessor->buildOutputDataArray($newCustomerDataObject, 'Magento\\Customer\\Api\\Data\\CustomerInterface'); $requestData = ['customer' => $newCustomerDataObject]; $expectedMessage = 'No such entity with %fieldName = %fieldValue'; try { $this->_webApiCall($serviceInfo, $requestData); $this->fail("Expected exception."); } catch (\SoapFault $e) { $this->assertContains($expectedMessage, $e->getMessage(), "SoapFault does not contain expected message."); } catch (\Exception $e) { $errorObj = $this->processRestExceptionResult($e); $this->assertEquals($expectedMessage, $errorObj['message']); $this->assertEquals(['fieldName' => 'customerId', 'fieldValue' => -1], $errorObj['parameters']); $this->assertEquals(HTTPExceptionCodes::HTTP_NOT_FOUND, $e->getCode()); } }
/** * @param string $formCode * @param RequestInterface $request * @return \Magento\Customer\Api\Data\CustomerInterface */ public function extract($formCode, RequestInterface $request) { $customerForm = $this->formFactory->create('customer', $formCode); $customerData = $customerForm->extractData($request); $allowedAttributes = $customerForm->getAllowedAttributes(); $isGroupIdEmpty = isset($allowedAttributes['group_id']); $customerDataObject = $this->customerFactory->create(); $this->dataObjectHelper->populateWithArray($customerDataObject, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $store = $this->storeManager->getStore(); if ($isGroupIdEmpty) { $customerDataObject->setGroupId($this->customerGroupManagement->getDefaultGroup($store->getId())->getId()); } $customerDataObject->setWebsiteId($store->getWebsiteId()); $customerDataObject->setStoreId($store->getId()); return $customerDataObject; }
/** * Retrieve customer object * * @return \Magento\Customer\Api\Data\CustomerInterface */ public function getCustomer() { if (!$this->customer) { $this->customer = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($this->customer, $this->_backendSession->getCustomerData()['account'], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); } return $this->customer; }
/** * @return \Magento\Customer\Api\Data\CustomerInterface */ private function _createCustomer() { /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */ $customer = $this->_customerFactory->create()->setFirstname('firstname')->setLastname('lastname')->setEmail('*****@*****.**'); $data = ['account' => $this->_dataObjectProcessor->buildOutputDataArray($customer, 'Magento\\Customer\\Api\\Data\\CustomerInterface')]; $this->_context->getBackendSession()->setCustomerData($data); return $customer; }
/** * Obtain customer data from session and create customer object * * @return \Magento\Customer\Api\Data\CustomerInterface */ protected function _getCustomerDataObject() { if ($this->_customerDataObject === null) { $customerData = $this->_backendSession->getCustomerData(); $accountData = isset($customerData['account']) ? $customerData['account'] : []; $this->_customerDataObject = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($this->_customerDataObject, $accountData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); } return $this->_customerDataObject; }
/** * @magentoAppArea frontend * @magentoDataFixture Magento/Customer/_files/customer.php * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php */ public function testUpdateCustomerDeleteAllAddressesWithEmptyArray() { $customerId = 1; $customer = $this->customerRepository->getById($customerId); $customerDetails = $customer->__toArray(); $newCustomerEntity = $this->customerFactory->create(); $this->dataObjectHelper->populateWithArray($newCustomerEntity, $customerDetails, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $newCustomerEntity->setId($customer->getId())->setAddresses([]); $this->customerRepository->save($newCustomerEntity); $newCustomerDetails = $this->customerRepository->getById($customerId); //Verify that old addresses are removed $this->assertEquals(0, count($newCustomerDetails->getAddresses())); }
/** * @Given customer accounts: */ public function customerAccounts(TableNode $customerAccountTable) { foreach ($customerAccountTable as $row) { // Check if the customer already exists. If it does, no need to // re-create it. If it doesn't, catch the exception and create // the customer account. try { $customer = $this->customerRepository->get($row['email_address']); } catch (NoSuchEntityException $e) { $customer = $this->customerFactory->create(); $customer->setFirstname($row['firstname'])->setLastname($row['lastname'])->setEmail($row['email_address']); $this->customerRepository->save($customer); } } }
/** * @magentoAppArea frontend * @magentoDataFixture Magento/Customer/_files/customer.php */ public function testCreateNewCustomerFromClone() { $email = '*****@*****.**'; $firstName = 'Firstsave'; $lastname = 'Lastsave'; $existingCustId = 1; $existingCustomer = $this->customerRepository->getById($existingCustId); $customerEntity = $this->customerFactory->create(); $this->dataObjectHelper->mergeDataObjects('\\Magento\\Customer\\Api\\Data\\CustomerInterface', $customerEntity, $existingCustomer); $customerEntity->setEmail($email)->setFirstname($firstName)->setLastname($lastname)->setId(null); $customer = $this->accountManagement->createAccount($customerEntity, 'aPassword'); $this->assertNotEmpty($customer->getId()); $this->assertEquals($email, $customer->getEmail()); $this->assertEquals($firstName, $customer->getFirstname()); $this->assertEquals($lastname, $customer->getLastname()); $this->accountManagement->authenticate($customer->getEmail(), 'aPassword', true); }
/** * Returns customer Data with customer group only * * @return \Magento\Customer\Api\Data\CustomerInterface */ protected function getDepersonalizedCustomer() { $customer = $this->customerFactory->create(); $customer->setGroupId($this->customerSession->getCustomerGroupId()); return $customer; }
/** * Validate customer data and set some its data for further usage in quote * * Will return either true or array with error messages * * @param array $data * @return bool|array */ protected function _validateCustomerData(array $data) { $quote = $this->getQuote(); $isCustomerNew = !$quote->getCustomerId(); $customer = $quote->getCustomer(); $customerData = $this->extensibleDataObjectConverter->toFlatArray($customer, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); /** @var Form $customerForm */ $customerForm = $this->_formFactory->create(\Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'checkout_register', $customerData, $this->_request->isAjax(), Form::IGNORE_INVISIBLE, []); if ($isCustomerNew) { $customerRequest = $customerForm->prepareRequest($data); $customerData = $customerForm->extractData($customerRequest); } $customerErrors = $customerForm->validateData($customerData); if ($customerErrors !== true) { return ['error' => -1, 'message' => implode(', ', $customerErrors)]; } if (!$isCustomerNew) { return true; } $customer = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($customer, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); if ($quote->getCheckoutMethod() == self::METHOD_REGISTER) { // We always have $customerRequest here, otherwise we would have been kicked off the function several // lines above $password = $customerRequest->getParam('customer_password'); if ($password != $customerRequest->getParam('confirm_password')) { return ['error' => -1, 'message' => __('Password and password confirmation are not equal.')]; } $quote->setPasswordHash($this->accountManagement->getPasswordHash($password)); } else { // set NOT LOGGED IN group id explicitly, // otherwise copyFieldsetToTarget('customer_account', 'to_quote') will fill it with default group id value $customer->setGroupId(GroupInterface::NOT_LOGGED_IN_ID); } //validate customer $result = $this->accountManagement->validate($customer); if (!$result->isValid()) { return ['error' => -1, 'message' => implode(', ', $result->getMessages())]; } // copy customer/guest email to address $quote->getBillingAddress()->setEmail($customer->getEmail()); // copy customer data to quote $this->_objectCopyService->copyFieldsetToTarget('customer_account', 'to_quote', $this->extensibleDataObjectConverter->toFlatArray($customer, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'), $quote); return true; }
/** * Add account data to quote * * @param array $accountData * @return $this */ public function setAccountData($accountData) { $customer = $this->getQuote()->getCustomer(); $form = $this->_createCustomerForm($customer); // emulate request $request = $form->prepareRequest($accountData); $data = $form->extractData($request); $data = $form->restoreData($data); $customer = $this->customerFactory->create(); $this->dataObjectHelper->populateWithArray($customer, $data, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $this->getQuote()->updateCustomerData($customer); $data = []; $customerData = $this->customerMapper->toFlatArray($customer); foreach ($form->getAttributes() as $attribute) { $code = sprintf('customer_%s', $attribute->getAttributeCode()); $data[$code] = isset($customerData[$attribute->getAttributeCode()]) ? $customerData[$attribute->getAttributeCode()] : null; } if (isset($data['customer_group_id'])) { $customerGroup = $this->groupRepository->getById($data['customer_group_id']); $data['customer_tax_class_id'] = $customerGroup->getTaxClassId(); $this->setRecollect(true); } $this->getQuote()->addData($data); return $this; }
/** * Retrieve customer model object * * @return \Magento\Customer\Api\Data\CustomerInterface|\Magento\Framework\Api\ExtensibleDataInterface */ public function getCustomer() { /** * @TODO: Remove the method after all external usages are refactored in MAGETWO-19930 * _customer and _customerFactory variables should be eliminated as well */ if (null === $this->_customer) { try { $this->_customer = $this->customerRepository->getById($this->getCustomerId()); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { $this->_customer = $this->customerDataFactory->create(); $this->_customer->setId(null); } } return $this->_customer; }
/** * @return \Magento\Customer\Api\Data\CustomerInterface */ protected function _getCustomer() { $customerDataObject = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($customerDataObject, $this->_backendSession->getCustomerData()['account'], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); return $customerDataObject; }
/** * Initialize form object * * @return $this * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function initForm() { $customerData = $this->_backendSession->getCustomerData(); /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(); $fieldset = $form->addFieldset('address_fieldset', ['legend' => __("Edit Customer's Address")]); $account = $customerData['account']; $address = $this->addressDataFactory->create(); if (!empty($account) && isset($account['store_id'])) { $address->setCountryId($this->_directoryHelper->getDefaultCountry($this->_storeManager->getStore($account['store_id']))); } else { $address->setCountryId($this->_directoryHelper->getDefaultCountry()); } $addressForm = $this->_metadataFormFactory->create('customer_address', 'adminhtml_customer_address', $this->addressMapper->toFlatArray($address)); $attributes = $addressForm->getAttributes(); if (isset($attributes['street'])) { if ($attributes['street']->getMultilineCount() <= 0) { $attributes['street']->setMultilineCount(self::DEFAULT_STREET_LINES_COUNT); } } foreach ($attributes as $key => $attribute) { $attributes[$key]->setFrontendLabel(__($attribute->getFrontendLabel()))->setIsVisible(false); } $this->_setFieldset($attributes, $fieldset); $regionElement = $form->getElement('region'); if ($regionElement) { $regionElement->setRenderer($this->_regionFactory->create()); } $regionElement = $form->getElement('region_id'); if ($regionElement) { $regionElement->setNoDisplay(true); } $country = $form->getElement('country_id'); if ($country) { $country->addClass('countries'); } $postcode = $form->getElement('postcode'); if ($postcode) { $postcode->removeClass('required-entry')->setRequired(!$this->_directoryHelper->isZipCodeOptional($address->getCountryId())); } if ($this->isReadonly()) { foreach ($this->_addressMetadataService->getAllAttributesMetadata() as $attribute) { $element = $form->getElement($attribute->getAttributeCode()); if ($element) { $element->setReadonly(true, true); } } } $customerStoreId = null; if (!empty($account) && isset($account['id']) && isset($account['website_id'])) { $customerStoreId = $this->_storeManager->getWebsite($account['website_id'])->getDefaultStore()->getId(); } $prefixElement = $form->getElement('prefix'); if ($prefixElement) { $prefixOptions = $this->options->getNamePrefixOptions($customerStoreId); if (!empty($prefixOptions)) { $fieldset->removeField($prefixElement->getId()); $prefixField = $fieldset->addField($prefixElement->getId(), 'select', $prefixElement->getData(), '^'); $prefixField->setValues($prefixOptions); } } $suffixElement = $form->getElement('suffix'); if ($suffixElement) { $suffixOptions = $this->options->getNameSuffixOptions($customerStoreId); if (!empty($suffixOptions)) { $fieldset->removeField($suffixElement->getId()); $suffixField = $fieldset->addField($suffixElement->getId(), 'select', $suffixElement->getData(), $form->getElement('lastname')->getId()); $suffixField->setValues($suffixOptions); } } $customerDataObject = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($customerDataObject, $account, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $this->assign('customer', $customerDataObject); $addressCollection = []; foreach ($customerData['address'] as $key => $addressData) { $addressDataObject = $this->addressDataFactory->create(); $this->dataObjectHelper->populateWithArray($addressDataObject, $addressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface'); $addressCollection[$key] = $addressDataObject; } $this->assign('addressCollection', $addressCollection); $form->setValues($this->addressMapper->toFlatArray($address)); $this->setForm($form); return $this; }
/** * {@inheritdoc} */ public function afterSave() { $customerData = (array) $this->getData(); $customerData[CustomerData::ID] = $this->getId(); $dataObject = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($dataObject, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $customerOrigData = (array) $this->getOrigData(); $customerOrigData[CustomerData::ID] = $this->getId(); $origDataObject = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($origDataObject, $customerOrigData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $this->_eventManager->dispatch('customer_save_after_data_object', ['customer_data_object' => $dataObject, 'orig_customer_data_object' => $origDataObject]); return parent::afterSave(); }
/** * Retrieve customer model with customer data * * @return \Magento\Customer\Api\Data\CustomerInterface */ public function getDataModel() { $customerData = $this->getData(); $addressesData = []; /** @var \Magento\Customer\Model\Address $address */ foreach ($this->getAddresses() as $address) { $addressesData[] = $address->getDataModel(); } $customerDataObject = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($customerDataObject, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $customerDataObject->setAddresses($addressesData)->setId($this->getId()); return $customerDataObject; }