Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save region correctly to save sales address from admin [backport] #11234

Merged
101 changes: 96 additions & 5 deletions app/code/Magento/Sales/Controller/Adminhtml/Order/AddressSave.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,32 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sales\Controller\Adminhtml\Order;

class AddressSave extends \Magento\Sales\Controller\Adminhtml\Order
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Directory\Model\RegionFactory;
use Magento\Sales\Api\OrderManagementInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Controller\Adminhtml\Order;
use Magento\Sales\Model\Order\Address;
use Psr\Log\LoggerInterface;
use Magento\Framework\Registry;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\Translate\InlineInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\View\Result\LayoutFactory;
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\App\ObjectManager;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AddressSave extends Order
{
/**
* Authorization level of a basic admin session
Expand All @@ -15,19 +38,71 @@ class AddressSave extends \Magento\Sales\Controller\Adminhtml\Order
*/
const ADMIN_RESOURCE = 'Magento_Sales::actions_edit';

/**
* @var RegionFactory
*/
private $regionFactory;

/**
* @param Context $context
* @param Registry $coreRegistry
* @param FileFactory $fileFactory
* @param InlineInterface $translateInline
* @param PageFactory $resultPageFactory
* @param JsonFactory $resultJsonFactory
* @param LayoutFactory $resultLayoutFactory
* @param RawFactory $resultRawFactory
* @param OrderManagementInterface $orderManagement
* @param OrderRepositoryInterface $orderRepository
* @param LoggerInterface $logger
* @param RegionFactory|null $regionFactory
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Context $context,
Registry $coreRegistry,
FileFactory $fileFactory,
InlineInterface $translateInline,
PageFactory $resultPageFactory,
JsonFactory $resultJsonFactory,
LayoutFactory $resultLayoutFactory,
RawFactory $resultRawFactory,
OrderManagementInterface $orderManagement,
OrderRepositoryInterface $orderRepository,
LoggerInterface $logger,
RegionFactory $regionFactory = null
) {
$this->regionFactory = $regionFactory ?: ObjectManager::getInstance()->get(RegionFactory::class);
parent::__construct(
$context,
$coreRegistry,
$fileFactory,
$translateInline,
$resultPageFactory,
$resultJsonFactory,
$resultLayoutFactory,
$resultRawFactory,
$orderManagement,
$orderRepository,
$logger
);
}

/**
* Save order address
*
* @return \Magento\Backend\Model\View\Result\Redirect
* @return Redirect
*/
public function execute()
{
$addressId = $this->getRequest()->getParam('address_id');
/** @var $address \Magento\Sales\Api\Data\OrderAddressInterface|\Magento\Sales\Model\Order\Address */
/** @var $address OrderAddressInterface|Address */
$address = $this->_objectManager->create(
\Magento\Sales\Api\Data\OrderAddressInterface::class
OrderAddressInterface::class
)->load($addressId);
$data = $this->getRequest()->getPostValue();
$data = $this->updateRegionData($data);
$resultRedirect = $this->resultRedirectFactory->create();
if ($data && $address->getId()) {
$address->addData($data);
Expand All @@ -41,7 +116,7 @@ public function execute()
);
$this->messageManager->addSuccess(__('You updated the order address.'));
return $resultRedirect->setPath('sales/*/view', ['order_id' => $address->getParentId()]);
} catch (\Magento\Framework\Exception\LocalizedException $e) {
} catch (LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addException($e, __('We can\'t update the order address right now.'));
Expand All @@ -51,4 +126,20 @@ public function execute()
return $resultRedirect->setPath('sales/*/');
}
}

/**
* Update region data
*
* @param array $attributeValues
* @return array
*/
private function updateRegionData($attributeValues)
{
if (!empty($attributeValues['region_id'])) {
$newRegion = $this->regionFactory->create()->load($attributeValues['region_id']);
$attributeValues['region_code'] = $newRegion->getCode();
$attributeValues['region'] = $newRegion->getDefaultName();
}
return $attributeValues;
}
}