Skip to content

Commit adebfa2

Browse files
committed
Save region correctly to save sales address from admin
1 parent 2e18329 commit adebfa2

File tree

1 file changed

+100
-5
lines changed

1 file changed

+100
-5
lines changed

app/code/Magento/Sales/Controller/Adminhtml/Order/AddressSave.php

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,104 @@
44
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
78
namespace 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;
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+
* @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+
}
1790

1891
/**
1992
* Save order address
2093
*
21-
* @return \Magento\Backend\Model\View\Result\Redirect
94+
* @return Redirect
2295
*/
2396
public function execute()
2497
{
2598
$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);
28103
$data = $this->getRequest()->getPostValue();
104+
$data = $this->updateRegionData($data);
29105
$resultRedirect = $this->resultRedirectFactory->create();
30106
if ($data && $address->getId()) {
31107
$address->addData($data);
@@ -38,15 +114,34 @@ public function execute()
38114
]
39115
);
40116
$this->messageManager->addSuccess(__('You updated the order address.'));
117+
41118
return $resultRedirect->setPath('sales/*/view', ['order_id' => $address->getParentId()]);
42-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
119+
} catch (LocalizedException $e) {
43120
$this->messageManager->addError($e->getMessage());
44121
} catch (\Exception $e) {
45122
$this->messageManager->addException($e, __('We can\'t update the order address right now.'));
46123
}
124+
47125
return $resultRedirect->setPath('sales/*/address', ['address_id' => $address->getId()]);
48126
} else {
49127
return $resultRedirect->setPath('sales/*/');
50128
}
51129
}
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+
}
52147
}

0 commit comments

Comments
 (0)