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