Skip to content

Commit fd3b83d

Browse files
authored
MAGETWO-80916: Save region correctly to save sales address from admin #11234
2 parents 6e49308 + bb1bcd6 commit fd3b83d

File tree

2 files changed

+97
-5
lines changed

2 files changed

+97
-5
lines changed

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

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,32 @@
44
* Copyright © 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
@@ -15,19 +38,71 @@ class AddressSave extends \Magento\Sales\Controller\Adminhtml\Order
1538
*/
1639
const ADMIN_RESOURCE = 'Magento_Sales::actions_edit';
1740

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+
}
91+
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 */
100+
/** @var $address OrderAddressInterface|Address */
27101
$address = $this->_objectManager->create(
28-
\Magento\Sales\Api\Data\OrderAddressInterface::class
102+
OrderAddressInterface::class
29103
)->load($addressId);
30104
$data = $this->getRequest()->getPostValue();
105+
$data = $this->updateRegionData($data);
31106
$resultRedirect = $this->resultRedirectFactory->create();
32107
if ($data && $address->getId()) {
33108
$address->addData($data);
@@ -41,7 +116,7 @@ public function execute()
41116
);
42117
$this->messageManager->addSuccess(__('You updated the order address.'));
43118
return $resultRedirect->setPath('sales/*/view', ['order_id' => $address->getParentId()]);
44-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
119+
} catch (LocalizedException $e) {
45120
$this->messageManager->addError($e->getMessage());
46121
} catch (\Exception $e) {
47122
$this->messageManager->addException($e, __('We can\'t update the order address right now.'));
@@ -51,4 +126,20 @@ public function execute()
51126
return $resultRedirect->setPath('sales/*/');
52127
}
53128
}
129+
130+
/**
131+
* Update region data
132+
*
133+
* @param array $attributeValues
134+
* @return array
135+
*/
136+
private function updateRegionData($attributeValues)
137+
{
138+
if (!empty($attributeValues['region_id'])) {
139+
$newRegion = $this->regionFactory->create()->load($attributeValues['region_id']);
140+
$attributeValues['region_code'] = $newRegion->getCode();
141+
$attributeValues['region'] = $newRegion->getDefaultName();
142+
}
143+
return $attributeValues;
144+
}
54145
}

dev/tests/functional/tests/app/Magento/Variable/Test/TestCase/UpdateCustomVariableEntityTest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<constraint name="Magento\Variable\Test\Constraint\AssertCustomVariableInPage" />
3333
</variation>
3434
<variation name="UpdateCustomVariableEntityTestVariation3">
35+
<data name="tag" xsi:type="string">stable:no</data>
3536
<data name="customVariable/data/code" xsi:type="string">variableCode%isolation%</data>
3637
<data name="customVariable/data/name" xsi:type="string">variableName%isolation%</data>
3738
<data name="customVariable/data/use_default_value" xsi:type="string">No</data>

0 commit comments

Comments
 (0)