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