/** * Two custom attributes are created, one for customer and another for customer address. * * Attribute related to customer address should be returned only. * * @magentoDataFixture Magento/Customer/_files/attribute_user_defined_address.php * @magentoDataFixture Magento/Customer/_files/attribute_user_defined_customer.php */ public function testGetCustomAttributesCodes() { $userDefinedAttributeCode = 'address_user_attribute'; $attributeCodes = $this->_addressBuilder->getCustomAttributesCodes(); $expectedAttributes = [$userDefinedAttributeCode]; $this->assertEquals($expectedAttributes, $attributeCodes, 'Custom attribute codes list is invalid.'); }
/** * {@inheritdoc} */ protected function _setDataValues(array $data) { $newData = array(); if (isset($data[CustomerDetails::KEY_CUSTOMER])) { $newData[CustomerDetails::KEY_CUSTOMER] = $this->_customerBuilder->populateWithArray($data[CustomerDetails::KEY_CUSTOMER])->create(); } if (isset($data[CustomerDetails::KEY_ADDRESSES])) { $newData[CustomerDetails::KEY_ADDRESSES] = array(); $addresses = $data[CustomerDetails::KEY_ADDRESSES]; foreach ($addresses as $address) { $newData[CustomerDetails::KEY_ADDRESSES][] = $this->_addressBuilder->populateWithArray($address)->create(); } } return parent::_setDataValues($newData); }
/** * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ public function testCreateAddressFromModelWithCustomerId() { $defaultBillingId = 1; $defaultShippingId = 1; $customerId = 1; $attributeCode = 'attribute_code'; $addressModelMock = $this->getAddressMockForCreate(); $addressModelMock->expects($this->once())->method('getId')->will($this->returnValue(null)); $addressModelMock->expects($this->any())->method('getCustomerId')->will($this->returnValue($customerId)); $addressModelMock->expects($this->any())->method('getParentId'); $getData = function ($key, $index = null) use($attributeCode, $customerId) { $result = null; switch ($key) { case $attributeCode: $result = 'some_data'; break; case 'customer_id': $result = $customerId; break; } return $result; }; $addressModelMock->expects($this->any())->method('getData')->will($this->returnCallback($getData)); $attributeMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Eav\\AttributeMetadata', array('getAttributeCode'), array(), '', false); $attributeMock->expects($this->once())->method('getAttributeCode')->will($this->returnValue($attributeCode)); $addressMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Address', array(), array(), '', false); $this->addressMetadataServiceMock->expects($this->once())->method('getAllAttributesMetadata')->will($this->returnValue(array($attributeMock))); $this->addressBuilderMock->expects($this->once())->method('create')->will($this->returnValue($addressMock)); $this->addressBuilderMock->expects($this->once())->method('setCustomerId')->with($this->equalTo($customerId)); $this->assertEquals($addressMock, $this->model->createAddressFromModel($addressModelMock, $defaultBillingId, $defaultShippingId)); }
/** * Add address to customer during create account * * @return \Magento\Customer\Service\V1\Data\Address|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 = array(); foreach ($allowedAttributes as $attribute) { $attributeCode = $attribute->getAttributeCode(); $value = $this->getRequest()->getParam($attributeCode); if (is_null($value)) { continue; } switch ($attributeCode) { case 'region_id': $this->_regionBuilder->setRegionId($value); break; case 'region': $this->_regionBuilder->setRegion($value); break; default: $addressData[$attributeCode] = $value; } } $this->_addressBuilder->populateWithArray($addressData); $this->_addressBuilder->setRegion($this->_regionBuilder->create()); $this->_addressBuilder->setDefaultBilling($this->getRequest()->getParam('default_billing', false))->setDefaultShipping($this->getRequest()->getParam('default_shipping', false)); return $this->_addressBuilder->create(); }
/** * @magentoAppArea frontend * @magentoDataFixture Magento/Customer/_files/customer_non_default_website_id.php */ public function testUpdateCustomerDetailsByEmail() { $customerId = 1; $firstName = 'Firstsave'; $lastName = 'Lastsave'; $newEmail = '*****@*****.**'; $city = 'San Jose'; $customerDetails = $this->_customerAccountService->getCustomerDetails($customerId); $website = Bootstrap::getObjectManager()->get('Magento\\Store\\Model\\Website'); $websiteId = $website->load('newwebsite')->getId(); $email = $customerDetails->getCustomer()->getEmail(); $customerData = array_merge($customerDetails->getCustomer()->__toArray(), ['firstname' => $firstName, 'lastname' => $lastName, 'email' => $newEmail, 'id' => null]); $addresses = $customerDetails->getAddresses(); $addressId = $addresses[0]->getId(); $newAddress = array_merge($addresses[0]->__toArray(), ['city' => $city]); $this->_customerBuilder->populateWithArray($customerData); $this->_addressBuilder->populateWithArray($newAddress); $this->_customerDetailsBuilder->setCustomer($this->_customerBuilder->create())->setAddresses([$this->_addressBuilder->create(), $addresses[1]]); $this->_customerAccountService->updateCustomerDetailsByEmail($email, $this->_customerDetailsBuilder->create(), $websiteId); $updatedCustomerDetails = $this->_customerAccountService->getCustomerDetails($customerId); $updateCustomerData = $updatedCustomerDetails->getCustomer(); $this->assertEquals($firstName, $updateCustomerData->getFirstname()); $this->assertEquals($lastName, $updateCustomerData->getLastname()); $this->assertEquals($newEmail, $updateCustomerData->getEmail()); $this->assertEquals(2, count($updatedCustomerDetails->getAddresses())); foreach ($updatedCustomerDetails->getAddresses() as $newAddress) { if ($newAddress->getId() == $addressId) { $this->assertEquals($city, $newAddress->getCity()); } } }
/** * Sample Address data * * @return array */ private function getSampleAddressEntity() { $regionBuilder = Bootstrap::getObjectManager()->create('\\Magento\\Customer\\Service\\V1\\Data\\RegionBuilder'); $this->_customerAddressBuilder->setCountryId('US')->setDefaultBilling(true)->setDefaultShipping(true)->setPostcode('75477')->setRegion($regionBuilder->setRegion('Alabama')->setRegionId(1)->setRegionCode('AL')->create())->setStreet(array('Green str, 67'))->setTelephone('3468676')->setCity('CityM')->setFirstname('John')->setLastname('Smith'); $address1 = $this->_customerAddressBuilder->create(); $this->_customerAddressBuilder->setCountryId('US')->setDefaultBilling(false)->setDefaultShipping(false)->setPostcode('47676')->setRegion($regionBuilder->setRegion('Alabama')->setRegionId(1)->setRegionCode('AL')->create())->setStreet(array('Black str, 48'))->setCity('CityX')->setTelephone('3234676')->setFirstname('John')->setLastname('Smith'); $address2 = $this->_customerAddressBuilder->create(); return array($address1, $address2); }
public function testSetCustomAttributes() { $customerAttributes = ['warehouse_zip' => [AttributeValue::ATTRIBUTE_CODE => 'warehouse_zip', AttributeValue::VALUE => '78777'], 'warehouse_alternate' => [AttributeValue::ATTRIBUTE_CODE => 'warehouse_alternate', AttributeValue::VALUE => '90051']]; $attributeValue1 = $this->_valueBuilder->populateWithArray($customerAttributes['warehouse_zip'])->create(); $attributeValue2 = $this->_valueBuilder->populateWithArray($customerAttributes['warehouse_alternate'])->create(); $address = $this->_addressBuilder->setCustomAttributes([$attributeValue1, $attributeValue2])->create(); $this->assertEquals('78777', $address->getCustomAttribute('warehouse_zip')->getValue()); $this->assertEquals('90051', $address->getCustomAttribute('warehouse_alternate')->getValue()); $this->assertEquals($customerAttributes, $address->__toArray()[Customer::CUSTOM_ATTRIBUTES_KEY]); }
/** * {@inheritdoc} */ protected function _setDataValues(array $data) { if (array_key_exists(QuoteDetails::KEY_BILLING_ADDRESS, $data)) { $data[QuoteDetails::KEY_BILLING_ADDRESS] = $this->addressBuilder->populateWithArray($data[QuoteDetails::KEY_BILLING_ADDRESS])->create(); } if (array_key_exists(QuoteDetails::KEY_SHIPPING_ADDRESS, $data)) { $data[QuoteDetails::KEY_SHIPPING_ADDRESS] = $this->addressBuilder->populateWithArray($data[QuoteDetails::KEY_SHIPPING_ADDRESS])->create(); } if (array_key_exists(QuoteDetails::KEY_ITEMS, $data)) { $items = []; foreach ($data[QuoteDetails::KEY_ITEMS] as $itemArray) { $items[] = $this->itemBuilder->populateWithArray($itemArray)->create(); } $data[QuoteDetails::KEY_ITEMS] = $items; } if (array_key_exists(QuoteDetails::KEY_CUSTOMER_TAX_CLASS_KEY, $data)) { $data[QuoteDetails::KEY_CUSTOMER_TAX_CLASS_KEY] = $this->taxClassKeyBuilder->populateWithArray($data[QuoteDetails::KEY_CUSTOMER_TAX_CLASS_KEY])->create(); } return parent::_setDataValues($data); }
/** * Make address Data Object out of an address model * * @param AbstractAddress $addressModel * @param int $defaultBillingId * @param int $defaultShippingId * @return Address */ public function createAddressFromModel(AbstractAddress $addressModel, $defaultBillingId, $defaultShippingId) { $addressId = $addressModel->getId(); $attributes = $this->_metadataService->getAllAddressAttributeMetadata(); $addressData = array(); foreach ($attributes as $attribute) { $code = $attribute->getAttributeCode(); if (!is_null($addressModel->getData($code))) { $addressData[$code] = $addressModel->getData($code); } } $isDefaultBilling = $addressModel->getData('is_default_billing') === null && intval($addressId) ? $addressId === $defaultBillingId : $addressModel->getData('is_default_billing'); $isDefaultShipping = $addressModel->getData('is_default_shipping') === null && intval($addressId) ? $addressId === $defaultShippingId : $addressModel->getData('is_default_shipping'); $this->_addressBuilder->populateWithArray(array_merge($addressData, array(Address::KEY_STREET => $addressModel->getStreet(), Address::KEY_DEFAULT_BILLING => $isDefaultBilling, Address::KEY_DEFAULT_SHIPPING => $isDefaultShipping, Address::KEY_REGION => array(Region::KEY_REGION => $addressModel->getRegion(), Region::KEY_REGION_ID => $addressModel->getRegionId(), Region::KEY_REGION_CODE => $addressModel->getRegionCode())))); if ($addressId) { $this->_addressBuilder->setId($addressId); } if ($addressModel->getCustomerId() || $addressModel->getParentId()) { $customerId = $addressModel->getCustomerId() ?: $addressModel->getParentId(); $this->_addressBuilder->setCustomerId($customerId); } $addressDataObject = $this->_addressBuilder->create(); return $addressDataObject; }
/** * 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->_addressService->getAddress($addressId); } catch (NoSuchEntityException $e) { } } if (is_null($this->_address) || !$this->_address->getId()) { $this->_address = $this->_addressBuilder->setPrefix($this->getCustomer()->getPrefix())->setFirstname($this->getCustomer()->getFirstname())->setMiddlename($this->getCustomer()->getMiddlename())->setLastname($this->getCustomer()->getLastname())->setSuffix($this->getCustomer()->getSuffix())->create(); } if ($headBlock = $this->getLayout()->getBlock('head')) { $headBlock->setTitle($this->getTitle()); } if ($postedData = $this->_customerSession->getAddressFormData(true)) { if (!empty($postedData['region_id']) || !empty($postedData['region'])) { $postedData['region'] = array('region_id' => $postedData['region_id'], 'region' => $postedData['region']); } $this->_address = $this->_addressBuilder->mergeDataObjectWithArray($this->_address, $postedData); } return $this; }
/** * 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', array('legend' => __("Edit Customer's Address"))); $account = $customerData['account']; $this->_addressBuilder->populateWithArray(array()); if (!empty($account) && isset($account['store_id'])) { $this->_addressBuilder->setCountryId($this->_coreData->getDefaultCountry($this->_storeManager->getStore($account['store_id']))); } else { $this->_addressBuilder->setCountryId($this->_coreData->getDefaultCountry()); } $address = $this->_addressBuilder->create(); $addressForm = $this->_metadataFormFactory->create('customer_address', 'adminhtml_customer_address', AddressConverter::toFlatArray($address)); $attributes = $addressForm->getAttributes(); if (isset($attributes['street'])) { if ($attributes['street']->getMultilineCount() <= 0) { $attributes['street'] = $this->_attributeMetadataBuilder->populate($attributes['street'])->setMultilineCount(self::DEFAULT_STREET_LINES_COUNT)->create(); } } foreach ($attributes as $key => $attribute) { $attributes[$key] = $this->_attributeMetadataBuilder->populate($attribute)->setFrontendLabel(__($attribute->getFrontendLabel()))->setVisible(false)->create(); } $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'); } if ($this->isReadonly()) { foreach ($this->_metadataService->getAllAddressAttributeMetadata() 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->_customerHelper->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->_customerHelper->getNameSuffixOptions($customerStoreId); if (!empty($suffixOptions)) { $fieldset->removeField($suffixElement->getId()); $suffixField = $fieldset->addField($suffixElement->getId(), 'select', $suffixElement->getData(), $form->getElement('lastname')->getId()); $suffixField->setValues($suffixOptions); } } $this->assign('customer', $this->_customerBuilder->populateWithArray($account)->create()); $addressCollection = array(); foreach ($customerData['address'] as $key => $addressData) { $addressCollection[$key] = $this->_addressBuilder->populateWithArray($addressData)->create(); } $this->assign('addressCollection', $addressCollection); $form->setValues(AddressConverter::toFlatArray($address)); $this->setForm($form); return $this; }
/** * Export data to customer address Data Object. * * @return \Magento\Customer\Service\V1\Data\Address */ public function exportCustomerAddressData() { $customerAddressData = $this->_objectCopyService->getDataFromFieldset('sales_convert_quote_address', 'to_customer_address', $this); $customerAddressDataWithRegion = array(); $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); return $this->_customerAddressBuilder->populateWithArray($customerAddressData)->create(); }
/** * Helper function that returns an Address Data Object that matches the data from customer_two_address fixture * * @return \Magento\Customer\Service\V1\Data\AddressBuilder */ private function _createSecondAddressBuilder() { return $this->_addressBuilder->populate($this->_expectedAddresses[1])->setId(null); }
public function testValidateAddressesBoth() { // Setup address mock, no first name $mockBadAddress = $this->_createAddress(1, ''); // Setup address mock, with first name $mockAddress = $this->_createAddress(1, 'John'); $addressBad = $this->_addressBuilder->create(); $addressGood = $this->_addressBuilder->create(); $this->_addressConverterMock->expects($this->any())->method('createAddressModel')->will($this->returnValueMap(array(array($addressBad, $mockBadAddress), array($addressGood, $mockAddress)))); $customerService = $this->_createService(); try { $customerService->validateAddresses(array($addressBad, $addressGood)); $this->fail("InputException was expected but not thrown"); } catch (InputException $actualException) { $expectedException = new InputException(); $expectedException->addError(InputException::REQUIRED_FIELD, ['fieldName' => 'firstname', 'index' => 0]); $this->assertEquals($expectedException, $actualException); } }
/** * Create customerAddressDataObject and save it in the Model\Quote so that it can be used to persist later. * * @param CustomerDataObject $customerDataObject * @param \Magento\Sales\Model\Quote\Address $quoteCustomerAddress * @return void * @throws \InvalidArgumentException */ protected function _prepareCustomerAddress($customerDataObject, $quoteCustomerAddress) { // Possible that customerId is null for new customers $customerId = $customerDataObject->getId(); $quoteCustomerAddress->setCustomerId($customerId); $customerAddressDataObject = $quoteCustomerAddress->exportCustomerAddressData(); $quoteAddressId = $quoteCustomerAddress->getCustomerAddressId(); $addressType = $quoteCustomerAddress->getAddressType(); if ($quoteAddressId) { /** Update existing address */ $existingAddressDataObject = $this->_customerAddressService->getAddress($quoteAddressId); /** Update customer address data */ $customerAddressDataObject = $this->_customerAddressBuilder->mergeDataObjects($existingAddressDataObject, $customerAddressDataObject); } elseif ($addressType == CustomerAddressDataObject::ADDRESS_TYPE_SHIPPING) { try { $billingAddressDataObject = $this->_customerAddressService->getDefaultBillingAddress($customerId); } catch (\Exception $e) { /** Billing address does not exist. */ } $isShippingAsBilling = $quoteCustomerAddress->getSameAsBilling(); if (isset($billingAddressDataObject) && $isShippingAsBilling) { /** Set existing billing address as default shipping */ $customerAddressDataObject = $this->_customerAddressBuilder->populate($billingAddressDataObject)->setDefaultShipping(true)->create(); } } switch ($addressType) { case CustomerAddressDataObject::ADDRESS_TYPE_BILLING: if (is_null($customerDataObject->getDefaultBilling())) { $customerAddressDataObject = $this->_customerAddressBuilder->populate($customerAddressDataObject)->setDefaultBilling(true)->create(); } break; case CustomerAddressDataObject::ADDRESS_TYPE_SHIPPING: if (is_null($customerDataObject->getDefaultShipping())) { $customerAddressDataObject = $this->_customerAddressBuilder->populate($customerAddressDataObject)->setDefaultShipping(true)->create(); } break; default: throw new \InvalidArgumentException('Customer address type is invalid.'); } $this->getQuote()->addCustomerAddressData($customerAddressDataObject); }
/** * Prepare quote for customer order submit * * @return void */ protected function _prepareCustomerQuote() { $quote = $this->getQuote(); $billing = $quote->getBillingAddress(); $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress(); $customer = $this->_customerAccountService->getCustomer($this->getCustomerSession()->getCustomerId()); $hasDefaultBilling = (bool) $customer->getDefaultBilling(); $hasDefaultShipping = (bool) $customer->getDefaultShipping(); if ($shipping && !$shipping->getSameAsBilling() && (!$shipping->getCustomerId() || $shipping->getSaveInAddressBook())) { $shippingAddress = $shipping->exportCustomerAddressData(); if (!$hasDefaultShipping) { //Make provided address as default shipping address $shippingAddress = $this->_addressBuilder->populate($shippingAddress)->setDefaultShipping(true)->create(); $hasDefaultShipping = true; } $quote->addCustomerAddressData($shippingAddress); $shipping->setCustomerAddressData($shippingAddress); } if (!$billing->getCustomerId() || $billing->getSaveInAddressBook()) { $billingAddress = $billing->exportCustomerAddressData(); if (!$hasDefaultBilling) { //Make provided address as default shipping address $this->_addressBuilder->populate($billingAddress); if (!$hasDefaultShipping) { //Make provided address as default shipping address $this->_addressBuilder->setDefaultShipping(true); } $this->_addressBuilder->setDefaultBilling(true); $billingAddress = $this->_addressBuilder->create(); } $quote->addCustomerAddressData($billingAddress); $billing->setCustomerAddressData($billingAddress); } }
/** * Map Address to Address data object * * @param AddressBuilder $addressBuilder * @param Address $address * @return \Magento\Customer\Service\V1\Data\Address */ public function mapAddress(AddressBuilder $addressBuilder, Address $address) { $addressBuilder->setCountryId($address->getCountryId()); $addressBuilder->setRegion($addressBuilder->getRegionBuilder()->setRegionId($address->getRegionId())->create()); $addressBuilder->setPostcode($address->getPostcode()); $addressBuilder->setCity($address->getCity()); $addressBuilder->setStreet($address->getStreet()); return $addressBuilder->create(); }