/** * 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; }
/** * {@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; }
/** * @return \Magento\Customer\Api\Data\AddressInterface|string */ public function execute() { $customerAddressData = $this->getRequest()->getParam('address'); $customerAddressDataWithRegion = []; $customerAddressDataWithRegion['region']['region'] = $customerAddressData['region']; if (isset($customerAddressData['region_code'])) { $customerAddressDataWithRegion['region']['region_code'] = $customerAddressData['region_code']; } if ($customerAddressData['region_id']) { $customerAddressDataWithRegion['region']['region_id'] = $customerAddressData['region_id']; } $customerAddressData = array_merge($customerAddressData, $customerAddressDataWithRegion); /** * @var \Magento\Customer\Api\Data\AddressInterface $addressDataObject */ $addressDataObject = $this->customerAddressFactory->create(); $this->dataObjectHelper->populateWithArray($addressDataObject, $customerAddressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface'); $addressValidationResponse = $this->validAddressManagement->saveValidAddress($addressDataObject, \Magento\Store\Model\Store::DEFAULT_STORE_ID); $resultJson = $this->resultJsonFactory->create(); if (!is_string($addressValidationResponse)) { $resultJson->setData([AddressInterface::FIRSTNAME => $addressValidationResponse->getFirstname(), AddressInterface::LASTNAME => $addressValidationResponse->getLastname(), AddressInterface::STREET => $addressValidationResponse->getStreet(), AddressInterface::COUNTRY_ID => $addressValidationResponse->getCountryId(), AddressInterface::CITY => $addressValidationResponse->getCity(), AddressInterface::REGION_ID => $addressValidationResponse->getRegionId(), AddressInterface::REGION => $addressValidationResponse->getRegion(), AddressInterface::POSTCODE => $addressValidationResponse->getPostcode()]); } else { $resultJson->setData($addressValidationResponse); } return $resultJson; }
public function testUpdateDataOverrideExistingData() { /** @var \Magento\Customer\Model\Data\Address $addressData */ $addressData = $this->addressFactory->create()->setId(2)->setCity('CityY')->setCompany('CompanyY')->setPostcode('88888'); $this->addressModel->updateData($addressData); /** @var \Magento\Customer\Model\Data\Address $addressData */ $updatedAddressData = $this->addressFactory->create()->setId(3)->setCity('CityZ')->setCompany('CompanyZ')->setPostcode('99999'); $updatedAddressData = $this->addressModel->updateData($updatedAddressData)->getDataModel(); $this->assertEquals(3, $updatedAddressData->getId()); $this->assertEquals('CityZ', $updatedAddressData->getCity()); $this->assertEquals('CompanyZ', $updatedAddressData->getCompany()); $this->assertEquals('99999', $updatedAddressData->getPostcode()); }
public function testPrepareQuoteForNewCustomer() { $customerAddressMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock(); $this->addressMock->expects($this->any())->method('exportCustomerAddress')->willReturn($customerAddressMock); $this->addressMock->expects($this->any())->method('getData')->willReturn([]); $this->addressFactoryMock->expects($this->any())->method('create')->willReturn($this->addressMock); $this->quoteMock->expects($this->any())->method('isVirtual')->willReturn(false); $this->quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($this->addressMock); $this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($this->addressMock); $customerDataMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock(); $this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerDataMock); $this->copyObjectMock->expects($this->any())->method('getDataFromFieldset')->willReturn([]); $this->dataObjectHelper->expects($this->any())->method('populateWithArray')->willReturnSelf(); $this->assertInstanceOf('Magento\\Quote\\Model\\Quote', $this->quote->prepareQuoteForNewCustomer($this->quoteMock)); }
/** * {@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']]); } } }
/** * @magentoAppArea frontend * @magentoDataFixture Magento/Customer/_files/customer.php * @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php */ public function testUpdateCustomerAddress() { $customerId = 1; $city = 'San Jose'; $email = '*****@*****.**'; $customer = $this->customerRepository->getById($customerId); $customerDetails = $customer->__toArray(); $addresses = $customer->getAddresses(); $addressId = $addresses[0]->getId(); $newAddress = array_merge($addresses[0]->__toArray(), ['city' => $city]); $newAddressDataObject = $this->addressFactory->create(); $this->dataObjectHelper->populateWithArray($newAddressDataObject, $newAddress, '\\Magento\\Customer\\Api\\Data\\AddressInterface'); $newAddressDataObject->setRegion($addresses[0]->getRegion()); $newCustomerEntity = $this->customerFactory->create(); $this->dataObjectHelper->populateWithArray($newCustomerEntity, $customerDetails, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $newCustomerEntity->setId($customerId)->setAddresses([$newAddressDataObject, $addresses[1]]); $this->customerRepository->save($newCustomerEntity); $newCustomer = $this->customerRepository->get($email); $this->assertEquals(2, count($newCustomer->getAddresses())); foreach ($newCustomer->getAddresses() as $newAddress) { if ($newAddress->getId() == $addressId) { $this->assertEquals($city, $newAddress->getCity()); } } }
/** * Prepare the layout of the address edit block. * * @return $this */ protected function _prepareLayout() { parent::_prepareLayout(); // Init address object if ($addressId = $this->getRequest()->getParam('id')) { try { $this->_address = $this->_addressRepository->getById($addressId); if ($this->_address->getCustomerId() != $this->_customerSession->getCustomerId()) { $this->_address = null; } } catch (NoSuchEntityException $e) { $this->_address = null; } } if ($this->_address === null || !$this->_address->getId()) { $this->_address = $this->addressDataFactory->create(); $customer = $this->getCustomer(); $this->_address->setPrefix($customer->getPrefix()); $this->_address->setFirstname($customer->getFirstname()); $this->_address->setMiddlename($customer->getMiddlename()); $this->_address->setLastname($customer->getLastname()); $this->_address->setSuffix($customer->getSuffix()); } $this->pageConfig->getTitle()->set($this->getTitle()); if ($postedData = $this->_customerSession->getAddressFormData(true)) { if (!empty($postedData['region_id']) || !empty($postedData['region'])) { $postedData['region'] = ['region_id' => $postedData['region_id'], 'region' => $postedData['region']]; } $this->dataObjectHelper->populateWithArray($this->_address, $postedData, '\\Magento\\Customer\\Api\\Data\\AddressInterface'); } return $this; }
public function testSetLayoutWithoutAddress() { $addressId = 1; $customerPrefix = 'prefix'; $customerFirstName = 'firstname'; $customerMiddlename = 'middlename'; $customerLastname = 'lastname'; $customerSuffix = 'suffix'; $title = 'title'; $layoutMock = $this->getMockBuilder('Magento\\Framework\\View\\LayoutInterface')->getMock(); $this->requestMock->expects($this->once())->method('getParam')->with('id', null)->willReturn($addressId); $this->addressRepositoryMock->expects($this->once())->method('getById')->with($addressId)->willThrowException(\Magento\Framework\Exception\NoSuchEntityException::singleField('addressId', $addressId)); $newAddressMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->getMock(); $this->addressDataFactoryMock->expects($this->once())->method('create')->willReturn($newAddressMock); $customerMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock(); $this->currentCustomerMock->expects($this->once())->method('getCustomer')->willReturn($customerMock); $customerMock->expects($this->once())->method('getPrefix')->willReturn($customerPrefix); $customerMock->expects($this->once())->method('getFirstname')->willReturn($customerFirstName); $customerMock->expects($this->once())->method('getMiddlename')->willReturn($customerMiddlename); $customerMock->expects($this->once())->method('getLastname')->willReturn($customerLastname); $customerMock->expects($this->once())->method('getSuffix')->willReturn($customerSuffix); $newAddressMock->expects($this->once())->method('setPrefix')->with($customerPrefix)->willReturnSelf(); $newAddressMock->expects($this->once())->method('setFirstname')->with($customerFirstName)->willReturnSelf(); $newAddressMock->expects($this->once())->method('setMiddlename')->with($customerMiddlename)->willReturnSelf(); $newAddressMock->expects($this->once())->method('setLastname')->with($customerLastname)->willReturnSelf(); $newAddressMock->expects($this->once())->method('setSuffix')->with($customerSuffix)->willReturnSelf(); $pageTitleMock = $this->getMockBuilder('Magento\\Framework\\View\\Page\\Title')->disableOriginalConstructor()->getMock(); $this->pageConfigMock->expects($this->once())->method('getTitle')->willReturn($pageTitleMock); $this->model->setData('title', $title); $pageTitleMock->expects($this->once())->method('set')->with($title)->willReturnSelf(); $this->assertEquals($this->model, $this->model->setLayout($layoutMock)); $this->assertEquals($layoutMock, $this->model->getLayout()); }
/** * 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; }
/** * @Given I have a new address with :street :city :region :zip :country */ public function iHaveANewAddressWith($street, $city, $region, $zip, $country) { $countryId = $this->getCountryId($country); $region = $this->loadRegionByName($region, $countryId); $address = $this->addressFactory->create()->setStreet((array) $street)->setCity($city)->setPostcode($zip)->setCountryId($countryId)->setRegion($region)->setRegionId($region->getRegionId())->setFirstname($this->customer->getFirstname())->setLastname($this->customer->getLastname())->setTelephone('555-555-5555')->setCustomerId($this->customer->getId()); $this->customerAddress = $address; }
/** * {@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('.'); } } }
/** * 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; }
/** * Helper function that returns an Address Data Object that matches the data from customer_two_address fixture * * @return \Magento\Customer\Api\Data\AddressInterface */ private function _createSecondAddress() { $address = $this->_addressFactory->create(); $this->dataObjectHelper->mergeDataObjects('\\Magento\\Customer\\Api\\Data\\AddressInterface', $address, $this->_expectedAddresses[1]); $address->setId(null); $address->setRegion($this->_expectedAddresses[1]->getRegion()); return $address; }
protected function setUp() { $this->objectManager = Bootstrap::getObjectManager(); $this->accountManagement = $this->objectManager->create('Magento\\Customer\\Api\\AccountManagementInterface'); $this->customerRepository = $this->objectManager->create('Magento\\Customer\\Api\\CustomerRepositoryInterface'); $this->addressRepository = $this->objectManager->create('Magento\\Customer\\Api\\AddressRepositoryInterface'); $this->addressFactory = $this->objectManager->create('Magento\\Customer\\Api\\Data\\AddressInterfaceFactory'); $this->customerFactory = $this->objectManager->create('Magento\\Customer\\Api\\Data\\CustomerInterfaceFactory'); $this->dataObjectHelper = $this->objectManager->create('Magento\\Framework\\Api\\DataObjectHelper'); $regionFactory = $this->objectManager->create('Magento\\Customer\\Api\\Data\\RegionInterfaceFactory'); $address = $this->addressFactory->create(); $address->setId('1')->setCountryId('US')->setCustomerId('1')->setPostcode('75477')->setRegion($regionFactory->create()->setRegionCode('AL')->setRegion('Alabama')->setRegionId(1))->setRegionId(1)->setCompany('CompanyName')->setStreet(['Green str, 67'])->setTelephone('3468676')->setCity('CityM')->setFirstname('John')->setLastname('Smith')->setIsDefaultShipping(true)->setIsDefaultBilling(true); $address2 = $this->addressFactory->create(); $address2->setId('2')->setCountryId('US')->setCustomerId('1')->setPostcode('47676')->setRegion($regionFactory->create()->setRegionCode('AL')->setRegion('Alabama')->setRegionId(1))->setRegionId(1)->setCompany('Company')->setStreet(['Black str, 48'])->setCity('CityX')->setTelephone('3234676')->setFirstname('John')->setLastname('Smith'); $this->_expectedAddresses = [$address, $address2]; $this->dataProcessor = $this->objectManager->create('Magento\\Framework\\Reflection\\DataObjectProcessor'); $this->extensibleDataObjectConverter = $this->objectManager->create('Magento\\Framework\\Api\\ExtensibleDataObjectConverter'); }
/** * 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; }
/** * Convert ValidAddress to CustomerAddressInterface * * @param \AvaTax\ValidAddress $address * @param CustomerAddressInterface $originalAddress * @return null|CustomerAddressInterface */ public function convertAvaTaxValidAddressToCustomerAddress(\AvaTax\ValidAddress $address, CustomerAddressInterface $originalAddress) { $street = []; if ($address->getLine1()) { $street[] = $address->getLine1(); } if ($address->getLine2()) { $street[] = $address->getLine2(); } if ($address->getLine3()) { $street[] = $address->getLine3(); } // Not using line 4, as it returns a concatenation of city, state, and zip (e.g., BAINBRIDGE IS WA 98110-2450) $region = $this->getRegionByCodeAndCountry($address->getRegion(), $address->getCountry()); if (is_null($region)) { return null; } $customerAddressData = $this->getCustomerAddressData($originalAddress); // Get data from original address so that information like name and telephone will be preserved $data = array_merge($customerAddressData, [CustomerAddressInterface::REGION => $region->getName(), CustomerAddressInterface::REGION_ID => $region->getId(), CustomerAddressInterface::COUNTRY_ID => $address->getCountry(), CustomerAddressInterface::STREET => $street, CustomerAddressInterface::POSTCODE => $address->getPostalCode(), CustomerAddressInterface::CITY => $address->getCity()]); return $this->customerAddressFactory->create(['data' => $data]); }
/** * 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; }
/** * 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; }
/** * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecuteWithNewCustomer() { $customerId = 22; $addressId = 11; $subscription = 'false'; $postValue = ['customer' => ['coolness' => false, 'disable_auto_group_change' => 'false'], 'address' => ['_template_' => '_template_', $addressId => ['entity_id' => $addressId, 'default_billing' => 'false', 'default_shipping' => 'false', 'code' => 'value', 'coolness' => false, 'region' => 'region', 'region_id' => 'region_id']], 'subscription' => $subscription]; $filteredData = ['coolness' => false, 'disable_auto_group_change' => 'false']; $addressFilteredData = ['entity_id' => $addressId, 'default_billing' => 'false', 'default_shipping' => 'false', 'code' => 'value', 'coolness' => false, 'region' => 'region', 'region_id' => 'region_id']; $mergedData = ['disable_auto_group_change' => 0, \Magento\Customer\Api\Data\CustomerInterface::DEFAULT_BILLING => null, \Magento\Customer\Api\Data\CustomerInterface::DEFAULT_SHIPPING => null, 'confirmation' => false]; $mergedAddressData = ['entity_id' => $addressId, 'default_billing' => false, 'default_shipping' => false, 'code' => 'value', 'region' => ['region' => 'region', 'region_id' => 'region_id'], 'region_id' => 'region_id', 'id' => $addressId]; /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $formMock */ $attributeMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface')->disableOriginalConstructor()->getMock(); $attributeMock->expects($this->exactly(2))->method('getAttributeCode')->willReturn('coolness'); $attributeMock->expects($this->exactly(2))->method('getFrontendInput')->willReturn('int'); $attributes = [$attributeMock]; $this->requestMock->expects($this->exactly(3))->method('getPostValue')->willReturn($postValue); $this->requestMock->expects($this->exactly(3))->method('getPost')->willReturnMap([['customer', null, $postValue['customer']], ['address', null, $postValue['address']], ['subscription', null, $subscription]]); /** @var \Magento\Customer\Model\Metadata\Form|\PHPUnit_Framework_MockObject_MockObject $formMock */ $formMock = $this->getMockBuilder('Magento\\Customer\\Model\\Metadata\\Form')->disableOriginalConstructor()->getMock(); $this->formFactoryMock->expects($this->exactly(2))->method('create')->willReturnMap([[\Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'adminhtml_customer', [], false, \Magento\Customer\Model\Metadata\Form::DONT_IGNORE_INVISIBLE, [], $formMock], [\Magento\Customer\Api\AddressMetadataInterface::ENTITY_TYPE_ADDRESS, 'adminhtml_customer_address', [], false, \Magento\Customer\Model\Metadata\Form::DONT_IGNORE_INVISIBLE, [], $formMock]]); $formMock->expects($this->exactly(2))->method('extractData')->willReturnMap([[$this->requestMock, 'customer', true, $filteredData], [$this->requestMock, 'address/' . $addressId, true, $addressFilteredData]]); /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */ $objectMock = $this->getMockBuilder('Magento\\Framework\\DataObject')->disableOriginalConstructor()->getMock(); $this->objectFactoryMock->expects($this->exactly(2))->method('create')->with(['data' => $postValue])->willReturn($objectMock); $objectMock->expects($this->exactly(2))->method('getData')->willReturnMap([['customer', null, $postValue['customer']], ['address/' . $addressId, null, $postValue['address'][$addressId]]]); $formMock->expects($this->exactly(2))->method('getAttributes')->willReturn($attributes); /** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */ $customerMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock(); $this->customerDataFactoryMock->expects($this->once())->method('create')->willReturn($customerMock); $addressMock = $this->getMockBuilder('\\Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock(); $this->addressDataFactoryMock->expects($this->once())->method('create')->willReturn($addressMock); $this->dataHelperMock->expects($this->exactly(2))->method('populateWithArray')->willReturnMap([[$customerMock, $mergedData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface', $this->dataHelperMock], [$addressMock, $mergedAddressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface', $this->dataHelperMock]]); $this->managementMock->expects($this->once())->method('createAccount')->with($customerMock, null, '')->willReturn($customerMock); $customerMock->expects($this->once())->method('getId')->willReturn($customerId); $this->authorizationMock->expects($this->once())->method('isAllowed')->with(null)->willReturn(true); /** @var \Magento\Newsletter\Model\Subscriber|\PHPUnit_Framework_MockObject_MockObject $subscriberMock */ $subscriberMock = $this->getMockBuilder('Magento\\Newsletter\\Model\\Subscriber')->disableOriginalConstructor()->getMock(); $this->subscriberFactoryMock->expects($this->once())->method('create')->with()->willReturn($subscriberMock); $subscriberMock->expects($this->once())->method('unsubscribeCustomerById')->with($customerId); $subscriberMock->expects($this->never())->method('subscribeCustomerById'); $this->sessionMock->expects($this->once())->method('unsCustomerData'); $this->registryMock->expects($this->once())->method('register')->with(\Magento\Customer\Controller\RegistryConstants::CURRENT_CUSTOMER_ID, $customerId); $this->messageManagerMock->expects($this->once())->method('addSuccess')->with(__('You saved the customer.'))->willReturnSelf(); $this->requestMock->expects($this->once())->method('getParam')->with('back', false)->willReturn(false); /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */ $redirectMock = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\Redirect')->disableOriginalConstructor()->getMock(); $this->redirectFactoryMock->expects($this->once())->method('create')->willReturn($redirectMock); $redirectMock->expects($this->once())->method('setPath')->with('customer/index', [])->willReturnSelf(); $this->assertEquals($redirectMock, $this->model->execute()); }
/** * Export data to customer address Data Object. * * @return \Magento\Customer\Api\Data\AddressInterface */ public function exportCustomerAddress() { $customerAddressData = $this->_objectCopyService->getDataFromFieldset('sales_convert_quote_address', 'to_customer_address', $this); $customerAddressDataWithRegion = []; $customerAddressDataWithRegion['region']['region'] = $customerAddressData['region']; if (isset($customerAddressData['region_code'])) { $customerAddressDataWithRegion['region']['region_code'] = $customerAddressData['region_code']; } if ($customerAddressData['region_id']) { $customerAddressDataWithRegion['region']['region_id'] = $customerAddressData['region_id']; } $customerAddressData = array_merge($customerAddressData, $customerAddressDataWithRegion); $addressDataObject = $this->addressDataFactory->create(); $this->dataObjectHelper->populateWithArray($addressDataObject, $customerAddressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface'); 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; }