Skip to content

Commit

Permalink
MAGETWO-80916: Save region correctly to save sales address from admin #…
Browse files Browse the repository at this point in the history
…11234

 - Merge Pull Request #11234 from raumatbel/magento2:FR#StateNotDisplayedSalesAdminAddress
 - Merged commits:
   1. 23efa60
   2. 9e7d3f4
   3. 8038e91
   4. ee56c70
   5. 08ffd20
   6. f524e64
   7. 7d2cfcd
   8. 900b05f
   9. ab90b21
  • Loading branch information
Volodymyr Kublytskyi committed Oct 9, 2017
2 parents 6e49308 + ab90b21 commit 0714d54
Showing 1 changed file with 96 additions and 5 deletions.
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
*/
protected 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;
}
}

0 comments on commit 0714d54

Please sign in to comment.