123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Block\Account\Dashboard;
- use Magento\Customer\Api\Data\AddressInterface;
- use Magento\Customer\Model\Address\Mapper;
- use Magento\Framework\Exception\NoSuchEntityException;
- /**
- * Class to manage customer dashboard addresses section
- *
- * @api
- * @since 100.0.2
- */
- class Address extends \Magento\Framework\View\Element\Template
- {
- /**
- * @var \Magento\Customer\Model\Address\Config
- */
- protected $_addressConfig;
- /**
- * @var \Magento\Customer\Helper\Session\CurrentCustomer
- */
- protected $currentCustomer;
- /**
- * @var \Magento\Customer\Helper\Session\CurrentCustomerAddress
- */
- protected $currentCustomerAddress;
- /**
- * @var Mapper
- */
- protected $addressMapper;
- /**
- * @param \Magento\Framework\View\Element\Template\Context $context
- * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
- * @param \Magento\Customer\Helper\Session\CurrentCustomerAddress $currentCustomerAddress
- * @param \Magento\Customer\Model\Address\Config $addressConfig
- * @param Mapper $addressMapper
- * @param array $data
- */
- public function __construct(
- \Magento\Framework\View\Element\Template\Context $context,
- \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
- \Magento\Customer\Helper\Session\CurrentCustomerAddress $currentCustomerAddress,
- \Magento\Customer\Model\Address\Config $addressConfig,
- Mapper $addressMapper,
- array $data = []
- ) {
- $this->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));
- }
- }
|