public function testToFlatArrayCustomAttributes()
 {
     $updatedAddressData = array('email' => '*****@*****.**', 'firstname' => 'John', 'lastname' => 'Doe', 'unknown_key' => 'Golden Necklace', 'custom_attributes' => ['warehouse_zip' => [AttributeValue::ATTRIBUTE_CODE => 'warehouse_zip', AttributeValue::VALUE => '78777'], 'warehouse_alternate' => [AttributeValue::ATTRIBUTE_CODE => 'warehouse_alternate', AttributeValue::VALUE => '90051']]);
     $expected = array('id' => 1, 'default_shipping' => false, 'default_billing' => true, 'firstname' => 'John', 'lastname' => 'Doe', 'street' => array('7700 W Parmer Ln'), 'city' => 'Austin', 'country_id' => 'US', 'region_id' => 1, 'region' => 'Texas', 'region_code' => 'TX', 'warehouse_zip' => '78777', 'warehouse_alternate' => '90051');
     $addressData = $this->_sampleAddressDataObject();
     $valueBuilder = $this->_objectManager->getObject('Magento\\Framework\\Service\\Data\\Eav\\AttributeValueBuilder');
     /** @var \Magento\Customer\Service\V1\Data\AddressBuilder $addressDataBuilder */
     $addressDataBuilder = $this->_objectManager->getObject('Magento\\Customer\\Service\\V1\\Data\\AddressBuilder', ['valueBuilder' => $valueBuilder, 'regionBuilder' => $this->_objectManager->getObject('\\Magento\\Customer\\Service\\V1\\Data\\RegionBuilder'), 'metadataService' => $this->_customerMetadataService]);
     $addressData = $addressDataBuilder->mergeDataObjectWithArray($addressData, $updatedAddressData);
     $result = AddressConverter::toFlatArray($addressData);
     $this->assertEquals($expected, $result);
 }
Example #2
0
 /**
  * Extract address from request
  *
  * @return \Magento\Customer\Service\V1\Data\Address
  */
 protected function _extractAddress()
 {
     $addressId = $this->getRequest()->getParam('id');
     $existingAddressData = array();
     if ($addressId) {
         $existingAddress = $this->_addressService->getAddress($addressId);
         if ($existingAddress->getId()) {
             $existingAddressData = \Magento\Customer\Service\V1\Data\AddressConverter::toFlatArray($existingAddress);
         }
     }
     /** @var \Magento\Customer\Model\Metadata\Form $addressForm */
     $addressForm = $this->_formFactory->create('customer_address', 'customer_address_edit', $existingAddressData);
     $addressData = $addressForm->extractData($this->getRequest());
     $attributeValues = $addressForm->compactData($addressData);
     $region = array('region_id' => $attributeValues['region_id'], 'region' => $attributeValues['region']);
     unset($attributeValues['region'], $attributeValues['region_id']);
     $attributeValues['region'] = $region;
     return $this->_addressBuilder->populateWithArray(array_merge($existingAddressData, $attributeValues))->setDefaultBilling($this->getRequest()->getParam('default_billing', false))->setDefaultShipping($this->getRequest()->getParam('default_shipping', false))->create();
 }
Example #3
0
 /**
  * {@inheritdoc}
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function render(AbstractAddress $address, $format = null)
 {
     $address = $this->_addressConverter->createAddressFromModel($address, 0, 0);
     return $this->renderArray(\Magento\Customer\Service\V1\Data\AddressConverter::toFlatArray($address), $format);
 }
Example #4
0
 /**
  * Represent customer address in 'online' format.
  *
  * @param \Magento\Customer\Service\V1\Data\Address $addressData
  * @return string
  */
 public function getAddressAsString($addressData)
 {
     $formatTypeRenderer = $this->_addressHelper->getFormatTypeRenderer('oneline');
     $result = '';
     if ($formatTypeRenderer) {
         $result = $formatTypeRenderer->renderArray(AddressConverter::toFlatArray($addressData));
     }
     return $this->escapeHtml($result);
 }
Example #5
0
 /**
  * Put existing customer data into the backend session
  */
 protected function setupExistingCustomerData()
 {
     /** @var Customer $customer */
     $customer = $this->_customerAccountService->getCustomer(1);
     $this->_customerData = array('customer_id' => $customer->getId(), 'account' => \Magento\Framework\Service\ExtensibleDataObjectConverter::toFlatArray($customer));
     $this->_customerData['account']['id'] = $customer->getId();
     /** @var Address[] $addresses */
     $addresses = $this->_addressService->getAddresses(1);
     foreach ($addresses as $addressData) {
         $this->_customerData['address'][$addressData->getId()] = AddressConverter::toFlatArray($addressData);
         $this->_customerData['address'][$addressData->getId()]['id'] = $addressData->getId();
     }
     $this->_backendSession->setCustomerData($this->_customerData);
 }
Example #6
0
 /**
  * Customer edit action
  *
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function execute()
 {
     $customerId = $this->_initCustomer();
     $this->_view->loadLayout();
     $this->_setActiveMenu('Magento_Customer::customer_manage');
     $customerData = array();
     $customerData['account'] = array();
     $customerData['address'] = array();
     $customer = null;
     $isExistingCustomer = (bool) $customerId;
     if ($isExistingCustomer) {
         try {
             $customer = $this->_customerAccountService->getCustomer($customerId);
             $customerData['account'] = ExtensibleDataObjectConverter::toFlatArray($customer);
             $customerData['account']['id'] = $customerId;
             try {
                 $addresses = $this->_addressService->getAddresses($customerId);
                 foreach ($addresses as $address) {
                     $customerData['address'][$address->getId()] = AddressConverter::toFlatArray($address);
                     $customerData['address'][$address->getId()]['id'] = $address->getId();
                 }
             } catch (NoSuchEntityException $e) {
                 //do nothing
             }
         } catch (NoSuchEntityException $e) {
             $this->messageManager->addException($e, __('An error occurred while editing the customer.'));
             $this->_redirect('customer/*/index');
             return;
         }
     }
     $customerData['customer_id'] = $customerId;
     // set entered data if was error when we do save
     $data = $this->_getSession()->getCustomerData(true);
     // restore data from SESSION
     if ($data && (!isset($data['customer_id']) || isset($data['customer_id']) && $data['customer_id'] == $customerId)) {
         $request = clone $this->getRequest();
         $request->setParams($data);
         if (isset($data['account']) && is_array($data['account'])) {
             $customerForm = $this->_formFactory->create('customer', 'adminhtml_customer', $customerData['account'], true);
             $formData = $customerForm->extractData($request, 'account');
             $customerData['account'] = $customerForm->restoreData($formData);
             $customer = $this->_customerBuilder->populateWithArray($customerData['account'])->create();
         }
         if (isset($data['address']) && is_array($data['address'])) {
             foreach (array_keys($data['address']) as $addressId) {
                 if ($addressId == '_template_') {
                     continue;
                 }
                 try {
                     $address = $this->_addressService->getAddress($addressId);
                     if (!empty($customerId) && $address->getCustomerId() == $customerId) {
                         $this->_addressBuilder->populate($address);
                     }
                 } catch (NoSuchEntityException $e) {
                     $this->_addressBuilder->setId($addressId);
                 }
                 if (!empty($customerId)) {
                     $this->_addressBuilder->setCustomerId($customerId);
                 }
                 $this->_addressBuilder->setDefaultBilling(!empty($data['account'][Customer::DEFAULT_BILLING]) && $data['account'][Customer::DEFAULT_BILLING] == $addressId);
                 $this->_addressBuilder->setDefaultShipping(!empty($data['account'][Customer::DEFAULT_SHIPPING]) && $data['account'][Customer::DEFAULT_SHIPPING] == $addressId);
                 $address = $this->_addressBuilder->create();
                 $requestScope = sprintf('address/%s', $addressId);
                 $addressForm = $this->_formFactory->create('customer_address', 'adminhtml_customer_address', AddressConverter::toFlatArray($address));
                 $formData = $addressForm->extractData($request, $requestScope);
                 $customerData['address'][$addressId] = $addressForm->restoreData($formData);
                 $customerData['address'][$addressId]['id'] = $addressId;
             }
         }
     }
     $this->_getSession()->setCustomerData($customerData);
     if ($isExistingCustomer) {
         $this->_title->add($this->_viewHelper->getCustomerName($customer));
     } else {
         $this->_title->add(__('New Customer'));
     }
     /**
      * Set active menu item
      */
     $this->_setActiveMenu('Magento_Customer::customer');
     $this->_view->renderLayout();
 }
Example #7
0
 /**
  * @param string $type
  * @return string
  */
 public function getAddressesHtmlSelect($type)
 {
     if ($this->isCustomerLoggedIn()) {
         $customerId = $this->_getCustomerData()->getId();
         $options = array();
         try {
             $addresses = $this->_customerAddressService->getAddresses($customerId);
         } catch (NoSuchEntityException $e) {
             $addresses = array();
         }
         foreach ($addresses as $address) {
             /** @var \Magento\Customer\Service\V1\Data\Address $address */
             $label = $this->_addressConfig->getFormatByCode(AddressConfig::DEFAULT_ADDRESS_FORMAT)->getRenderer()->renderArray(\Magento\Customer\Service\V1\Data\AddressConverter::toFlatArray($address));
             $options[] = array('value' => $address->getId(), 'label' => $label);
         }
         $addressId = $this->getAddress()->getCustomerAddressId();
         if (empty($addressId)) {
             try {
                 if ($type == 'billing') {
                     $address = $this->_customerAddressService->getDefaultBillingAddress($customerId);
                 } else {
                     $address = $this->_customerAddressService->getDefaultShippingAddress($customerId);
                 }
                 if ($address) {
                     $addressId = $address->getId();
                 }
             } catch (NoSuchEntityException $e) {
                 // Do nothing
             }
         }
         $select = $this->getLayout()->createBlock('Magento\\Framework\\View\\Element\\Html\\Select')->setName($type . '_address_id')->setId($type . '-address-select')->setClass('address-select')->setValue($addressId)->setOptions($options);
         $select->addOption('', __('New Address'));
         return $select->getHtml();
     }
     return '';
 }
Example #8
0
 /**
  * Retrieve options for addresses dropdown
  *
  * @return array
  */
 public function getAddressOptions()
 {
     $options = $this->getData('address_options');
     if (is_null($options)) {
         $options = [];
         $addresses = [];
         try {
             $addresses = $this->_customerAddressService->getAddresses($this->getCustomerId());
         } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
             /** Customer does not exist */
         }
         /** @var \Magento\Customer\Service\V1\Data\Address $address */
         foreach ($addresses as $address) {
             $label = $this->_addressConfig->getFormatByCode(AddressConfig::DEFAULT_ADDRESS_FORMAT)->getRenderer()->renderArray(\Magento\Customer\Service\V1\Data\AddressConverter::toFlatArray($address));
             $options[] = ['value' => $address->getId(), 'label' => $label];
         }
         $this->setData('address_options', $options);
     }
     return $options;
 }
Example #9
0
 /**
  * Import quote address data from customer address Data Object.
  *
  * @param \Magento\Customer\Service\V1\Data\Address $address
  * @return $this
  */
 public function importCustomerAddressData(\Magento\Customer\Service\V1\Data\Address $address)
 {
     $this->_objectCopyService->copyFieldsetToTarget('customer_address', 'to_quote_address', AddressConverter::toFlatArray($address), $this);
     $region = $this->getRegion();
     if (isset($region['region_id']) && isset($region['region'])) {
         $this->setRegionId($region['region_id']);
         $this->setRegion($region['region']);
     }
     $quote = $this->getQuote();
     if ($address->getCustomerId() && (!empty($quote) && $address->getCustomerId() == $quote->getCustomerId())) {
         $customer = $quote->getCustomerData();
         $this->setEmail($customer->getEmail());
     }
     return $this;
 }
Example #10
0
 /**
  * Updates an Address Model based on information from an Address Data Object.
  *
  * @param AddressModel $addressModel
  * @param Address $address
  * @return void
  */
 public function updateAddressModel(AddressModel $addressModel, Address $address)
 {
     // Set all attributes
     $attributes = AddressConverter::toFlatArray($address);
     foreach ($attributes as $attributeCode => $attributeData) {
         if (Address::KEY_REGION === $attributeCode && $address->getRegion() instanceof Region) {
             $addressModel->setDataUsingMethod(Region::KEY_REGION, $address->getRegion()->getRegion());
             $addressModel->setDataUsingMethod(Region::KEY_REGION_CODE, $address->getRegion()->getRegionCode());
             $addressModel->setDataUsingMethod(Region::KEY_REGION_ID, $address->getRegion()->getRegionId());
         } else {
             $addressModel->setDataUsingMethod($attributeCode, $attributeData);
         }
     }
     // Set customer related data
     $isBilling = $address->isDefaultBilling();
     $addressModel->setIsDefaultBilling($isBilling);
     $addressModel->setIsDefaultShipping($address->isDefaultShipping());
     // Need to use attribute set or future updates can cause data loss
     if (!$addressModel->getAttributeSetId()) {
         $addressModel->setAttributeSetId(CustomerMetadataServiceInterface::ATTRIBUTE_SET_ID_ADDRESS);
     }
 }
Example #11
0
 /**
  * Format the given address to the given type
  *
  * @param Address $address
  * @param string $type
  * @return string
  */
 public function format(Address $address, $type)
 {
     return $this->_addressHelper->getFormatTypeRenderer($type)->renderArray(AddressConverter::toFlatArray($address));
 }
Example #12
0
 /**
  * Render an address as HTML and return the result
  *
  * @param \Magento\Customer\Service\V1\Data\Address $address
  * @return string
  */
 public function getAddressHtml(\Magento\Customer\Service\V1\Data\Address $address = null)
 {
     if (!is_null($address)) {
         /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */
         $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer();
         return $renderer->renderArray(\Magento\Customer\Service\V1\Data\AddressConverter::toFlatArray($address));
     }
     return '';
 }
Example #13
0
 /**
  * @return string|null
  */
 public function getBillingAddressHtml()
 {
     try {
         $address = $this->_addressService->getAddress($this->getCustomer()->getDefaultBilling());
     } catch (NoSuchEntityException $e) {
         return __('The customer does not have default billing address.');
     }
     return $this->_addressHelper->getFormatTypeRenderer('html')->renderArray(AddressConverter::toFlatArray($address));
 }
Example #14
0
 /**
  * Render an address as HTML and return the result
  *
  * @param \Magento\Customer\Service\V1\Data\Address $address
  * @return string
  */
 protected function _getAddressHtml($address)
 {
     /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */
     $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer();
     return $renderer->renderArray(AddressConverter::toFlatArray($address));
 }
 /**
  * @magentoDataFixture  Magento/Customer/_files/customer.php
  * @magentoDataFixture  Magento/Customer/_files/customer_address.php
  * @magentoAppIsolation enabled
  */
 public function testSaveNewInvalidAddresses()
 {
     $firstAddressBuilder = $this->_addressBuilder->populateWithArray(array_merge(AddressConverter::toFlatArray($this->_expectedAddresses[0]), array('firstname' => null)))->setId(null);
     $firstAddress = $firstAddressBuilder->create();
     $secondAddressBuilder = $this->_addressBuilder->populateWithArray(array_merge(AddressConverter::toFlatArray($this->_expectedAddresses[0]), array('lastname' => null)))->setId(null);
     $secondAddress = $secondAddressBuilder->create();
     $customerId = 1;
     try {
         $this->_service->saveAddresses($customerId, array($firstAddress, $secondAddress));
         $this->fail("Expected NoSuchEntityException not caught");
     } catch (InputException $exception) {
         $this->assertEquals(InputException::DEFAULT_MESSAGE, $exception->getMessage());
         $errors = $exception->getErrors();
         $this->assertCount(2, $errors);
         $this->assertEquals('firstname is a required field.', $errors[0]->getLogMessage());
         $this->assertEquals('lastname is a required field.', $errors[1]->getLogMessage());
     }
 }
Example #16
0
 /**
  * Get order data jason
  *
  * @return string
  */
 public function getOrderDataJson()
 {
     $data = array();
     if ($this->getCustomerId()) {
         $data['customer_id'] = $this->getCustomerId();
         $data['addresses'] = array();
         $addresses = $this->_addressService->getAddresses($this->getCustomerId());
         foreach ($addresses as $addressData) {
             $addressForm = $this->_customerFormFactory->create('customer_address', 'adminhtml_customer_address', AddressConverter::toFlatArray($addressData));
             $data['addresses'][$addressData->getId()] = $addressForm->outputData(\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON);
         }
     }
     if (!is_null($this->getStoreId())) {
         $data['store_id'] = $this->getStoreId();
         $currency = $this->_localeCurrency->getCurrency($this->getStore()->getCurrentCurrencyCode());
         $symbol = $currency->getSymbol() ? $currency->getSymbol() : $currency->getShortName();
         $data['currency_symbol'] = $symbol;
         $data['shipping_method_reseted'] = !(bool) $this->getQuote()->getShippingAddress()->getShippingMethod();
         $data['payment_method'] = $this->getQuote()->getPayment()->getMethod();
     }
     return $this->_jsonEncoder->encode($data);
 }
Example #17
0
 /**
  * Represent customer address in HTML format.
  *
  * @param \Magento\Customer\Service\V1\Data\Address $addressData
  * @return string
  */
 public function getAddressAsHtml($addressData)
 {
     $formatTypeRenderer = $this->_customerAddressHelper->getFormatTypeRenderer('html');
     $result = '';
     if ($formatTypeRenderer) {
         $result = $formatTypeRenderer->renderArray(AddressConverter::toFlatArray($addressData));
     }
     return $result;
 }