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 as ModelOrderAddress ;
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
+ /**
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
+ }
17
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 */
27
- $ address = $ this ->_objectManager ->create ('Magento\Sales\Api\Data\OrderAddressInterface ' )->load ($ addressId );
100
+ /** @var $address OrderAddressInterface|ModelOrderAddress */
101
+ $ address = $ this ->_objectManager ->create (
102
+ OrderAddressInterface::class
103
+ )->load ($ addressId );
28
104
$ data = $ this ->getRequest ()->getPostValue ();
105
+ $ data = $ this ->updateRegionData ($ data );
29
106
$ resultRedirect = $ this ->resultRedirectFactory ->create ();
30
107
if ($ data && $ address ->getId ()) {
31
108
$ address ->addData ($ data );
@@ -38,15 +115,34 @@ public function execute()
38
115
]
39
116
);
40
117
$ this ->messageManager ->addSuccess (__ ('You updated the order address. ' ));
118
+
41
119
return $ resultRedirect ->setPath ('sales/*/view ' , ['order_id ' => $ address ->getParentId ()]);
42
- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
120
+ } catch (LocalizedException $ e ) {
43
121
$ this ->messageManager ->addError ($ e ->getMessage ());
44
122
} catch (\Exception $ e ) {
45
123
$ this ->messageManager ->addException ($ e , __ ('We can \'t update the order address right now. ' ));
46
124
}
125
+
47
126
return $ resultRedirect ->setPath ('sales/*/address ' , ['address_id ' => $ address ->getId ()]);
48
127
} else {
49
128
return $ resultRedirect ->setPath ('sales/*/ ' );
50
129
}
51
130
}
131
+
132
+ /**
133
+ * Update region data
134
+ *
135
+ * @param array $attributeValues
136
+ * @return array
137
+ */
138
+ private function updateRegionData ($ attributeValues )
139
+ {
140
+ if (!empty ($ attributeValues ['region_id ' ])) {
141
+ $ newRegion = $ this ->regionFactory ->create ()->load ($ attributeValues ['region_id ' ]);
142
+ $ attributeValues ['region_code ' ] = $ newRegion ->getCode ();
143
+ $ attributeValues ['region ' ] = $ newRegion ->getDefaultName ();
144
+ }
145
+
146
+ return $ attributeValues ;
147
+ }
52
148
}
0 commit comments