Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddressSave.php
Go to the documentation of this file.
1 <?php
9 
18 use Psr\Log\LoggerInterface;
25 use Magento\Framework\Controller\Result\RawFactory;
28 
32 class AddressSave extends Order
33 {
39  const ADMIN_RESOURCE = 'Magento_Sales::actions_edit';
40 
44  private $regionFactory;
45 
62  public function __construct(
63  Context $context,
64  Registry $coreRegistry,
65  FileFactory $fileFactory,
66  InlineInterface $translateInline,
70  RawFactory $resultRawFactory,
73  LoggerInterface $logger,
74  RegionFactory $regionFactory = null
75  ) {
76  $this->regionFactory = $regionFactory ?: ObjectManager::getInstance()->get(RegionFactory::class);
77  parent::__construct(
78  $context,
79  $coreRegistry,
80  $fileFactory,
81  $translateInline,
88  $logger
89  );
90  }
91 
97  public function execute()
98  {
99  $addressId = $this->getRequest()->getParam('address_id');
101  $address = $this->_objectManager->create(
102  OrderAddressInterface::class
103  )->load($addressId);
104  $data = $this->getRequest()->getPostValue();
105  $data = $this->updateRegionData($data);
106  $resultRedirect = $this->resultRedirectFactory->create();
107  if ($data && $address->getId()) {
108  $address->addData($data);
109  try {
110  $address->save();
111  $this->_eventManager->dispatch(
112  'admin_sales_order_address_update',
113  [
114  'order_id' => $address->getParentId()
115  ]
116  );
117  $this->messageManager->addSuccessMessage(__('You updated the order address.'));
118  return $resultRedirect->setPath('sales/*/view', ['order_id' => $address->getParentId()]);
119  } catch (LocalizedException $e) {
120  $this->messageManager->addErrorMessage($e->getMessage());
121  } catch (\Exception $e) {
122  $this->messageManager->addExceptionMessage($e, __('We can\'t update the order address right now.'));
123  }
124  return $resultRedirect->setPath('sales/*/address', ['address_id' => $address->getId()]);
125  } else {
126  return $resultRedirect->setPath('sales/*/');
127  }
128  }
129 
136  private function updateRegionData($attributeValues)
137  {
138  if (!empty($attributeValues['region_id'])) {
139  $newRegion = $this->regionFactory->create()->load($attributeValues['region_id']);
140  $attributeValues['region_code'] = $newRegion->getCode();
141  $attributeValues['region'] = $newRegion->getDefaultName();
142  }
143  return $attributeValues;
144  }
145 }
__()
Definition: __.php:13
$address
Definition: customer.php:38
__construct(Context $context, Registry $coreRegistry, FileFactory $fileFactory, InlineInterface $translateInline, PageFactory $resultPageFactory, JsonFactory $resultJsonFactory, LayoutFactory $resultLayoutFactory, RawFactory $resultRawFactory, OrderManagementInterface $orderManagement, OrderRepositoryInterface $orderRepository, LoggerInterface $logger, RegionFactory $regionFactory=null)
Definition: AddressSave.php:62