Skip to content

Commit 8486408

Browse files
author
Oleksii Korshenko
authored
MAGETWO-81675: Save region correctly to save sales address from admin #11381
2 parents 50fd5c1 + 2b78a06 commit 8486408

File tree

1 file changed

+94
-5
lines changed

1 file changed

+94
-5
lines changed

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

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,29 @@
66
*/
77
namespace Magento\Sales\Controller\Adminhtml\Order;
88

9-
class AddressSave extends \Magento\Sales\Controller\Adminhtml\Order
9+
use Magento\Backend\App\Action\Context;
10+
use Magento\Backend\Model\View\Result\Redirect;
11+
use Magento\Directory\Model\RegionFactory;
12+
use Magento\Sales\Api\OrderManagementInterface;
13+
use Magento\Sales\Api\OrderRepositoryInterface;
14+
use Magento\Sales\Api\Data\OrderAddressInterface;
15+
use Magento\Sales\Controller\Adminhtml\Order;
16+
use Magento\Sales\Model\Order\Address as AddressModel;
17+
use Psr\Log\LoggerInterface;
18+
use Magento\Framework\Registry;
19+
use Magento\Framework\App\Response\Http\FileFactory;
20+
use Magento\Framework\Translate\InlineInterface;
21+
use Magento\Framework\View\Result\PageFactory;
22+
use Magento\Framework\Controller\Result\JsonFactory;
23+
use Magento\Framework\View\Result\LayoutFactory;
24+
use Magento\Framework\Controller\Result\RawFactory;
25+
use Magento\Framework\Exception\LocalizedException;
26+
use Magento\Framework\App\ObjectManager;
27+
28+
/**
29+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
30+
*/
31+
class AddressSave extends Order
1032
{
1133
/**
1234
* Authorization level of a basic admin session
@@ -15,19 +37,70 @@ class AddressSave extends \Magento\Sales\Controller\Adminhtml\Order
1537
*/
1638
const ADMIN_RESOURCE = 'Magento_Sales::actions_edit';
1739

40+
/**
41+
* @var RegionFactory
42+
*/
43+
private $regionFactory;
44+
/**
45+
* @param Context $context
46+
* @param Registry $coreRegistry
47+
* @param FileFactory $fileFactory
48+
* @param InlineInterface $translateInline
49+
* @param PageFactory $resultPageFactory
50+
* @param JsonFactory $resultJsonFactory
51+
* @param LayoutFactory $resultLayoutFactory
52+
* @param RawFactory $resultRawFactory
53+
* @param OrderManagementInterface $orderManagement
54+
* @param OrderRepositoryInterface $orderRepository
55+
* @param LoggerInterface $logger
56+
* @param RegionFactory|null $regionFactory
57+
*
58+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
59+
*/
60+
public function __construct(
61+
Context $context,
62+
Registry $coreRegistry,
63+
FileFactory $fileFactory,
64+
InlineInterface $translateInline,
65+
PageFactory $resultPageFactory,
66+
JsonFactory $resultJsonFactory,
67+
LayoutFactory $resultLayoutFactory,
68+
RawFactory $resultRawFactory,
69+
OrderManagementInterface $orderManagement,
70+
OrderRepositoryInterface $orderRepository,
71+
LoggerInterface $logger,
72+
RegionFactory $regionFactory = null
73+
) {
74+
$this->regionFactory = $regionFactory ?: ObjectManager::getInstance()->get(RegionFactory::class);
75+
parent::__construct(
76+
$context,
77+
$coreRegistry,
78+
$fileFactory,
79+
$translateInline,
80+
$resultPageFactory,
81+
$resultJsonFactory,
82+
$resultLayoutFactory,
83+
$resultRawFactory,
84+
$orderManagement,
85+
$orderRepository,
86+
$logger
87+
);
88+
}
89+
1890
/**
1991
* Save order address
2092
*
21-
* @return \Magento\Backend\Model\View\Result\Redirect
93+
* @return Redirect
2294
*/
2395
public function execute()
2496
{
2597
$addressId = $this->getRequest()->getParam('address_id');
26-
/** @var $address \Magento\Sales\Api\Data\OrderAddressInterface|\Magento\Sales\Model\Order\Address */
98+
/** @var $address OrderAddressInterface|AddressModel */
2799
$address = $this->_objectManager->create(
28-
\Magento\Sales\Api\Data\OrderAddressInterface::class
100+
OrderAddressInterface::class
29101
)->load($addressId);
30102
$data = $this->getRequest()->getPostValue();
103+
$data = $this->updateRegionData($data);
31104
$resultRedirect = $this->resultRedirectFactory->create();
32105
if ($data && $address->getId()) {
33106
$address->addData($data);
@@ -41,7 +114,7 @@ public function execute()
41114
);
42115
$this->messageManager->addSuccess(__('You updated the order address.'));
43116
return $resultRedirect->setPath('sales/*/view', ['order_id' => $address->getParentId()]);
44-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
117+
} catch (LocalizedException $e) {
45118
$this->messageManager->addError($e->getMessage());
46119
} catch (\Exception $e) {
47120
$this->messageManager->addException($e, __('We can\'t update the order address right now.'));
@@ -51,4 +124,20 @@ public function execute()
51124
return $resultRedirect->setPath('sales/*/');
52125
}
53126
}
127+
128+
/**
129+
* Update region data
130+
*
131+
* @param array $attributeValues
132+
* @return array
133+
*/
134+
private function updateRegionData($attributeValues)
135+
{
136+
if (!empty($attributeValues['region_id'])) {
137+
$newRegion = $this->regionFactory->create()->load($attributeValues['region_id']);
138+
$attributeValues['region_code'] = $newRegion->getCode();
139+
$attributeValues['region'] = $newRegion->getDefaultName();
140+
}
141+
return $attributeValues;
142+
}
54143
}

0 commit comments

Comments
 (0)