_customerSession = $customerSession; $this->_addressRepository = $addressRepository; $this->addressDataFactory = $addressDataFactory; $this->currentCustomer = $currentCustomer; $this->dataObjectHelper = $dataObjectHelper; $this->addressMetadata = $addressMetadata; parent::__construct( $context, $directoryHelper, $jsonEncoder, $configCacheType, $regionCollectionFactory, $countryCollectionFactory, $data ); } /** * Prepare the layout of the address edit block. * * @return $this */ protected function _prepareLayout() { parent::_prepareLayout(); $this->initAddressObject(); $this->pageConfig->getTitle()->set($this->getTitle()); if ($postedData = $this->_customerSession->getAddressFormData(true)) { $postedData['region'] = [ 'region_id' => isset($postedData['region_id']) ? $postedData['region_id'] : null, 'region' => $postedData['region'], ]; $this->dataObjectHelper->populateWithArray( $this->_address, $postedData, \Magento\Customer\Api\Data\AddressInterface::class ); } $this->precheckRequiredAttributes(); return $this; } /** * Initialize address object. * * @return void */ private function initAddressObject() { // Init address object if ($addressId = $this->getRequest()->getParam('id')) { try { $this->_address = $this->_addressRepository->getById($addressId); if ($this->_address->getCustomerId() != $this->_customerSession->getCustomerId()) { $this->_address = null; } } catch (NoSuchEntityException $e) { $this->_address = null; } } if ($this->_address === null || !$this->_address->getId()) { $this->_address = $this->addressDataFactory->create(); $customer = $this->getCustomer(); $this->_address->setPrefix($customer->getPrefix()); $this->_address->setFirstname($customer->getFirstname()); $this->_address->setMiddlename($customer->getMiddlename()); $this->_address->setLastname($customer->getLastname()); $this->_address->setSuffix($customer->getSuffix()); } } /** * Precheck attributes that may be required in attribute configuration. * * @return void */ private function precheckRequiredAttributes() { $precheckAttributes = $this->getData('check_attributes_on_render'); $requiredAttributesPrechecked = []; if (!empty($precheckAttributes) && is_array($precheckAttributes)) { foreach ($precheckAttributes as $attributeCode) { $attributeMetadata = $this->addressMetadata->getAttributeMetadata($attributeCode); if ($attributeMetadata && $attributeMetadata->isRequired()) { $requiredAttributesPrechecked[$attributeCode] = $attributeCode; } } } $this->setData('required_attributes_prechecked', $requiredAttributesPrechecked); } /** * Generate name block html. * * @return string */ public function getNameBlockHtml() { $nameBlock = $this->getLayout() ->createBlock(\Magento\Customer\Block\Widget\Name::class) ->setObject($this->getAddress()); return $nameBlock->toHtml(); } /** * Return the title, either editing an existing address, or adding a new one. * * @return string */ public function getTitle() { if ($title = $this->getData('title')) { return $title; } if ($this->getAddress()->getId()) { $title = __('Edit Address'); } else { $title = __('Add New Address'); } return $title; } /** * Return the Url to go back. * * @return string */ public function getBackUrl() { if ($this->getData('back_url')) { return $this->getData('back_url'); } if ($this->getCustomerAddressCount()) { return $this->getUrl('customer/address'); } else { return $this->getUrl('customer/account/'); } } /** * Return the Url for saving. * * @return string */ public function getSaveUrl() { return $this->_urlBuilder->getUrl( 'customer/address/formPost', ['_secure' => true, 'id' => $this->getAddress()->getId()] ); } /** * Return the associated address. * * @return \Magento\Customer\Api\Data\AddressInterface */ public function getAddress() { return $this->_address; } /** * Return the specified numbered street line. * * @param int $lineNumber * @return string */ public function getStreetLine($lineNumber) { $street = $this->_address->getStreet(); return isset($street[$lineNumber - 1]) ? $street[$lineNumber - 1] : ''; } /** * Return the country Id. * * @return int|null|string */ public function getCountryId() { if ($countryId = $this->getAddress()->getCountryId()) { return $countryId; } return parent::getCountryId(); } /** * Return the name of the region for the address being edited. * * @return string region name */ public function getRegion() { $region = $this->getAddress()->getRegion(); return $region === null ? '' : $region->getRegion(); } /** * Return the id of the region being edited. * * @return int region id */ public function getRegionId() { $region = $this->getAddress()->getRegion(); return $region === null ? 0 : $region->getRegionId(); } /** * Retrieve the number of addresses associated with the customer given a customer Id. * * @return int */ public function getCustomerAddressCount() { return count($this->getCustomer()->getAddresses()); } /** * Determine if the address can be set as the default billing address. * * @return bool|int */ public function canSetAsDefaultBilling() { if (!$this->getAddress()->getId()) { return $this->getCustomerAddressCount(); } return !$this->isDefaultBilling(); } /** * Determine if the address can be set as the default shipping address. * * @return bool|int */ public function canSetAsDefaultShipping() { if (!$this->getAddress()->getId()) { return $this->getCustomerAddressCount(); } return !$this->isDefaultShipping(); } /** * Is the address the default billing address? * * @return bool */ public function isDefaultBilling() { return (bool)$this->getAddress()->isDefaultBilling(); } /** * Is the address the default shipping address? * * @return bool */ public function isDefaultShipping() { return (bool)$this->getAddress()->isDefaultShipping(); } /** * Retrieve the Customer Data using the customer Id from the customer session. * * @return \Magento\Customer\Api\Data\CustomerInterface */ public function getCustomer() { return $this->currentCustomer->getCustomer(); } /** * Return back button Url, either to customer address or account. * * @return string */ public function getBackButtonUrl() { if ($this->getCustomerAddressCount()) { return $this->getUrl('customer/address'); } else { return $this->getUrl('customer/account/'); } } /** * Get config value. * * @param string $path * @return string|null */ public function getConfig($path) { return $this->_scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); } }