currentCustomer = $currentCustomer; $this->currentCustomerAddress = $currentCustomerAddress; $this->_addressConfig = $addressConfig; parent::__construct($context, $data); $this->addressMapper = $addressMapper; } /** * Get the logged in customer * * @return \Magento\Customer\Api\Data\CustomerInterface|null */ public function getCustomer() { try { return $this->currentCustomer->getCustomer(); } catch (NoSuchEntityException $e) { return null; } } /** * HTML for Shipping Address * * @return \Magento\Framework\Phrase|string */ public function getPrimaryShippingAddressHtml() { try { $address = $this->currentCustomerAddress->getDefaultShippingAddress(); } catch (NoSuchEntityException $e) { return __('You have not set a default shipping address.'); } if ($address) { return $this->_getAddressHtml($address); } else { return __('You have not set a default shipping address.'); } } /** * HTML for Billing Address * * @return \Magento\Framework\Phrase|string */ public function getPrimaryBillingAddressHtml() { try { $address = $this->currentCustomerAddress->getDefaultBillingAddress(); } catch (NoSuchEntityException $e) { return __('You have not set a default billing address.'); } if ($address) { return $this->_getAddressHtml($address); } else { return __('You have not set a default billing address.'); } } /** * @return string */ public function getPrimaryShippingAddressEditUrl() { if (!$this->getCustomer()) { return ''; } else { $address = $this->currentCustomerAddress->getDefaultShippingAddress(); $addressId = $address ? $address->getId() : null; return $this->_urlBuilder->getUrl( 'customer/address/edit', ['id' => $addressId] ); } } /** * @return string */ public function getPrimaryBillingAddressEditUrl() { if (!$this->getCustomer()) { return ''; } else { $address = $this->currentCustomerAddress->getDefaultBillingAddress(); $addressId = $address ? $address->getId() : null; return $this->_urlBuilder->getUrl( 'customer/address/edit', ['id' => $addressId] ); } } /** * @return string */ public function getAddressBookUrl() { return $this->getUrl('customer/address/'); } /** * 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)); } }