/
AddressSave.php
148 lines (138 loc) · 4.87 KB
/
AddressSave.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Controller\Adminhtml\Order;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Directory\Model\RegionFactory;
use Magento\Sales\Api\OrderManagementInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Controller\Adminhtml\Order;
use Magento\Sales\Model\Order\Address as ModelOrderAddress;
use Psr\Log\LoggerInterface;
use Magento\Framework\Registry;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\Translate\InlineInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\View\Result\LayoutFactory;
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\App\ObjectManager;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AddressSave extends Order
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_Sales::actions_edit';
/**
* @var RegionFactory
*/
private $regionFactory;
/**
* @param Context $context
* @param Registry $coreRegistry
* @param FileFactory $fileFactory
* @param InlineInterface $translateInline
* @param PageFactory $resultPageFactory
* @param JsonFactory $resultJsonFactory
* @param LayoutFactory $resultLayoutFactory
* @param RawFactory $resultRawFactory
* @param OrderManagementInterface $orderManagement
* @param OrderRepositoryInterface $orderRepository
* @param LoggerInterface $logger
* @param RegionFactory|null $regionFactory
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __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
) {
$this->regionFactory = $regionFactory ?: ObjectManager::getInstance()->get(RegionFactory::class);
parent::__construct(
$context,
$coreRegistry,
$fileFactory,
$translateInline,
$resultPageFactory,
$resultJsonFactory,
$resultLayoutFactory,
$resultRawFactory,
$orderManagement,
$orderRepository,
$logger
);
}
/**
* Save order address
*
* @return Redirect
*/
public function execute()
{
$addressId = $this->getRequest()->getParam('address_id');
/** @var $address OrderAddressInterface|ModelOrderAddress */
$address = $this->_objectManager->create(
OrderAddressInterface::class
)->load($addressId);
$data = $this->getRequest()->getPostValue();
$data = $this->updateRegionData($data);
$resultRedirect = $this->resultRedirectFactory->create();
if ($data && $address->getId()) {
$address->addData($data);
try {
$address->save();
$this->_eventManager->dispatch(
'admin_sales_order_address_update',
[
'order_id' => $address->getParentId()
]
);
$this->messageManager->addSuccess(__('You updated the order address.'));
return $resultRedirect->setPath('sales/*/view', ['order_id' => $address->getParentId()]);
} catch (LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addException($e, __('We can\'t update the order address right now.'));
}
return $resultRedirect->setPath('sales/*/address', ['address_id' => $address->getId()]);
} else {
return $resultRedirect->setPath('sales/*/');
}
}
/**
* Update region data
*
* @param array $attributeValues
* @return array
*/
private function updateRegionData($attributeValues)
{
if (!empty($attributeValues['region_id'])) {
$newRegion = $this->regionFactory->create()->load($attributeValues['region_id']);
$attributeValues['region_code'] = $newRegion->getCode();
$attributeValues['region'] = $newRegion->getDefaultName();
}
return $attributeValues;
}
}