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