/** * @dataProvider renderDataProvider */ public function testRender($address, $format, $expected) { /** @var DefaultRenderer $renderer */ $renderer = $this->_addressConfig->getFormatByCode($format)->getRenderer(); $actual = $renderer->render($address); $this->assertEquals($expected, $actual); }
/** * Format address in a specific way * * @param Address $address * @param string $type * @return string|null */ public function format(Address $address, $type) { $formatType = $this->addressConfig->getFormatByCode($type); if (!$formatType || !$formatType->getRenderer()) { return null; } $this->eventManager->dispatch('customer_address_format', ['type' => $formatType, 'address' => $address]); return $formatType->getRenderer()->renderArray($address->getData()); }
public function testGetFormats() { $this->_storeMock->expects($this->once())->method('getId'); $this->_scopeConfigMock->expects($this->any())->method('getValue')->will($this->returnValue('someValue')); $rendererMock = $this->getMock('Magento\\Framework\\Object'); $this->_addressHelperMock->expects($this->any())->method('getRenderer')->will($this->returnValue($rendererMock)); $firstExpected = new \Magento\Framework\Object(); $firstExpected->setCode('format_one')->setTitle('format_one_title')->setDefaultFormat('someValue')->setEscapeHtml(false)->setRenderer(null); $secondExpected = new \Magento\Framework\Object(); $secondExpected->setCode('format_two')->setTitle('format_two_title')->setDefaultFormat('someValue')->setEscapeHtml(true)->setRenderer(null); $expectedResult = [$firstExpected, $secondExpected]; $this->assertEquals($expectedResult, $this->_model->getFormats()); }
/** * Get HTML output for specified address * * @param \Magento\Quote\Model\Quote\Address $address * @return string */ public function renderAddress($address) { /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */ $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer(); $addressData = \Magento\Framework\Convert\ConvertArray::toFlatArray($address->getData()); return $renderer->renderArray($addressData); }
/** * @param string $type * @return string */ public function getAddressesHtmlSelect($type) { if ($this->isCustomerLoggedIn()) { $options = []; try { $addresses = $this->_getCustomer()->getAddresses(); } catch (NoSuchEntityException $e) { $addresses = []; } foreach ($addresses as $address) { $builtOutputAddressData = $this->addressMapper->toFlatArray($address); $label = $this->_addressConfig->getFormatByCode(AddressConfig::DEFAULT_ADDRESS_FORMAT)->getRenderer()->renderArray($builtOutputAddressData); $options[] = ['value' => $address->getId(), 'label' => $label]; } $addressId = $this->getAddress()->getCustomerAddressId(); if (empty($addressId)) { try { if ($type == 'billing') { $addressId = $this->_getCustomer()->getDefaultBilling(); } else { $addressId = $this->_getCustomer()->getDefaultShipping(); } } 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 ''; }
/** * Retrieve renderer by code * * @param string $code * @return \Magento\Customer\Block\Address\Renderer\RendererInterface|null */ public function getFormatTypeRenderer($code) { $formatType = $this->_addressConfig->getFormatByCode($code); if (!$formatType || !$formatType->getRenderer()) { return null; } return $formatType->getRenderer(); }
/** * Render an address as HTML and return the result * * @param \Magento\Customer\Api\Data\AddressInterface $address * @return string */ public function getAddressHtml(\Magento\Customer\Api\Data\AddressInterface $address = null) { if ($address !== null) { /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */ $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer(); return $renderer->renderArray($this->addressMapper->toFlatArray($address)); } return ''; }
/** * @param string $code * @param array $result * @dataProvider getFormatDataProvider */ public function testGetFormat($code, $result) { if ($result) { $renderer = $this->getMockBuilder('Magento\\Customer\\Block\\Address\\Renderer\\RendererInterface')->disableOriginalConstructor()->getMock(); $renderer->expects($this->once())->method('getFormatArray')->will($this->returnValue(array('key' => 'value'))); } $this->addressConfig->expects($this->once())->method('getFormatByCode')->with($code)->will($this->returnValue(new \Magento\Framework\Object(!empty($result) ? array('renderer' => $renderer) : array()))); $this->assertEquals($result, $this->helper->getFormat($code)); }
/** * 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 ''; }
/** * 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; }
/** * Retrieve options for addresses dropdown * * @return array */ public function getAddressOptions() { $options = $this->getData('address_options'); if ($options === null) { $options = []; $addresses = []; try { $addresses = $this->customerRepository->getById($this->getCustomerId())->getAddresses(); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { /** Customer does not exist */ } /** @var \Magento\Customer\Api\Data\AddressInterface $address */ foreach ($addresses as $address) { $label = $this->_addressConfig->getFormatByCode(AddressConfig::DEFAULT_ADDRESS_FORMAT)->getRenderer()->renderArray($this->addressMapper->toFlatArray($address)); $options[] = ['value' => $address->getId(), 'label' => $label]; } $this->setData('address_options', $options); } return $options; }
/** * Render an address as HTML and return the result * * @param AddressInterface $address * @return string */ protected function _getAddressHtml($address) { /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */ $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer(); return $renderer->renderArray($this->addressMapper->toFlatArray($address)); }
/** * @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 ''; }
/** * Set additional customer address data * * @param \Magento\Customer\Api\Data\AddressInterface $address * @return string */ private function getCustomerAddressInline($address) { $builtOutputAddressData = $this->addressMapper->toFlatArray($address); return $this->addressConfig->getFormatByCode(\Magento\Customer\Model\Address\Config::DEFAULT_ADDRESS_FORMAT)->getRenderer()->renderArray($builtOutputAddressData); }