11 use Magento\Customer\Api\Data\AddressInterfaceFactory;
13 use Magento\Customer\Api\Data\RegionInterfaceFactory;
22 use Magento\Framework\Controller\Result\ForwardFactory;
46 private $customerAddressMapper;
67 FormKeyValidator $formKeyValidator,
101 protected function _extractAddress()
106 $addressForm = $this->_formFactory->create(
108 'customer_address_edit',
116 $addressDataObject = $this->addressDataFactory->create();
117 $this->dataObjectHelper->populateWithArray(
120 \
Magento\Customer\Api\Data\AddressInterface::class
122 $addressDataObject->setCustomerId($this->
_getSession()->getCustomerId())
123 ->setIsDefaultBilling($this->
getRequest()->getParam(
'default_billing',
false))
124 ->setIsDefaultShipping($this->
getRequest()->getParam(
'default_shipping',
false));
126 return $addressDataObject;
137 $existingAddressData = [];
138 if ($addressId = $this->
getRequest()->getParam(
'id')) {
139 $existingAddress = $this->_addressRepository->getById($addressId);
140 if ($existingAddress->getCustomerId() !== $this->
_getSession()->getCustomerId()) {
141 throw new \Exception();
143 $existingAddressData = $this->getCustomerAddressMapper()->toFlatArray($existingAddress);
145 return $existingAddressData;
158 $newRegion = $this->regionFactory->create()->load(
$attributeValues[
'region_id']);
171 $region = $this->regionDataFactory->create();
172 $this->dataObjectHelper->populateWithArray(
175 \
Magento\Customer\Api\Data\RegionInterface::class
188 if (!$this->_formKeyValidator->validate($this->getRequest())) {
189 return $this->resultRedirectFactory->create()->setPath(
'*/*/');
194 return $this->resultRedirectFactory->create()->setUrl(
195 $this->
_redirect->error($this->_buildUrl(
'*/*/edit'))
200 $address = $this->_extractAddress();
201 $this->_addressRepository->save(
$address);
202 $this->messageManager->addSuccessMessage(
__(
'You saved the address.'));
203 $url = $this->_buildUrl(
'*/*/index', [
'_secure' =>
true]);
204 return $this->resultRedirectFactory->create()->setUrl($this->
_redirect->success(
$url));
206 $this->messageManager->addErrorMessage($e->getMessage());
208 $this->messageManager->addErrorMessage($error->getMessage());
210 }
catch (\Exception $e) {
211 $redirectUrl = $this->_buildUrl(
'*/*/index');
212 $this->messageManager->addExceptionMessage($e,
__(
'We can\'t save the address.'));
218 $url = $this->_buildUrl(
'*/*/edit', [
'id' => $this->
getRequest()->getParam(
'id')]);
221 return $this->resultRedirectFactory->create()->setUrl($this->
_redirect->error(
$url));
231 private function getCustomerAddressMapper()
233 if ($this->customerAddressMapper ===
null) {
238 return $this->customerAddressMapper;
_redirect($path, $arguments=[])
updateRegionData(&$attributeValues)
__construct(Context $context, Session $customerSession, FormKeyValidator $formKeyValidator, FormFactory $formFactory, AddressRepositoryInterface $addressRepository, AddressInterfaceFactory $addressDataFactory, RegionInterfaceFactory $regionDataFactory, DataObjectProcessor $dataProcessor, DataObjectHelper $dataObjectHelper, ForwardFactory $resultForwardFactory, PageFactory $resultPageFactory, RegionFactory $regionFactory, HelperData $helperData)