Skip to content

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

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 101 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,28 +4,105 @@
* Copyright © 2013-2017 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 as ModelOrderAddress;
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
*
* @see _isAllowed()
*/
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 */
$address = $this->_objectManager->create('Magento\Sales\Api\Data\OrderAddressInterface')->load($addressId);
/** @var $address OrderAddressInterface|ModelOrderAddress */
$address = $this->_objectManager->create(
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 @@ -38,15 +115,34 @@ 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.'));
}

return $resultRedirect->setPath('sales/*/address', ['address_id' => $address->getId()]);
} else {
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;
}
}