44 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
55 * See COPYING.txt for license details.
66 */
7+
78namespace Magento \Sales \Controller \Adminhtml \Order ;
89
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
1033{
1134 /**
1235 * Authorization level of a basic admin session
1336 *
1437 * @see _isAllowed()
1538 */
1639 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+ }
1791
1892 /**
1993 * Save order address
2094 *
21- * @return \Magento\Backend\Model\View\Result\ Redirect
95+ * @return Redirect
2296 */
2397 public function execute ()
2498 {
2599 $ 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 );
28104 $ data = $ this ->getRequest ()->getPostValue ();
105+ $ data = $ this ->updateRegionData ($ data );
29106 $ resultRedirect = $ this ->resultRedirectFactory ->create ();
30107 if ($ data && $ address ->getId ()) {
31108 $ address ->addData ($ data );
@@ -38,15 +115,34 @@ public function execute()
38115 ]
39116 );
40117 $ this ->messageManager ->addSuccess (__ ('You updated the order address. ' ));
118+
41119 return $ resultRedirect ->setPath ('sales/*/view ' , ['order_id ' => $ address ->getParentId ()]);
42- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
120+ } catch (LocalizedException $ e ) {
43121 $ this ->messageManager ->addError ($ e ->getMessage ());
44122 } catch (\Exception $ e ) {
45123 $ this ->messageManager ->addException ($ e , __ ('We can \'t update the order address right now. ' ));
46124 }
125+
47126 return $ resultRedirect ->setPath ('sales/*/address ' , ['address_id ' => $ address ->getId ()]);
48127 } else {
49128 return $ resultRedirect ->setPath ('sales/*/ ' );
50129 }
51130 }
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+ }
52148}
0 commit comments