AddressSave.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Sales\Controller\Adminhtml\Order;
  8. use Magento\Backend\App\Action\Context;
  9. use Magento\Backend\Model\View\Result\Redirect;
  10. use Magento\Directory\Model\RegionFactory;
  11. use Magento\Sales\Api\OrderManagementInterface;
  12. use Magento\Sales\Api\OrderRepositoryInterface;
  13. use Magento\Sales\Api\Data\OrderAddressInterface;
  14. use Magento\Sales\Controller\Adminhtml\Order;
  15. use Magento\Sales\Model\Order\Address as AddressModel;
  16. use Psr\Log\LoggerInterface;
  17. use Magento\Framework\Registry;
  18. use Magento\Framework\App\Response\Http\FileFactory;
  19. use Magento\Framework\Translate\InlineInterface;
  20. use Magento\Framework\View\Result\PageFactory;
  21. use Magento\Framework\Controller\Result\JsonFactory;
  22. use Magento\Framework\View\Result\LayoutFactory;
  23. use Magento\Framework\Controller\Result\RawFactory;
  24. use Magento\Framework\Exception\LocalizedException;
  25. use Magento\Framework\App\ObjectManager;
  26. /**
  27. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  28. */
  29. class AddressSave extends Order
  30. {
  31. /**
  32. * Authorization level of a basic admin session
  33. *
  34. * @see _isAllowed()
  35. */
  36. const ADMIN_RESOURCE = 'Magento_Sales::actions_edit';
  37. /**
  38. * @var RegionFactory
  39. */
  40. private $regionFactory;
  41. /**
  42. * @param Context $context
  43. * @param Registry $coreRegistry
  44. * @param FileFactory $fileFactory
  45. * @param InlineInterface $translateInline
  46. * @param PageFactory $resultPageFactory
  47. * @param JsonFactory $resultJsonFactory
  48. * @param LayoutFactory $resultLayoutFactory
  49. * @param RawFactory $resultRawFactory
  50. * @param OrderManagementInterface $orderManagement
  51. * @param OrderRepositoryInterface $orderRepository
  52. * @param LoggerInterface $logger
  53. * @param RegionFactory|null $regionFactory
  54. *
  55. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  56. */
  57. public function __construct(
  58. Context $context,
  59. Registry $coreRegistry,
  60. FileFactory $fileFactory,
  61. InlineInterface $translateInline,
  62. PageFactory $resultPageFactory,
  63. JsonFactory $resultJsonFactory,
  64. LayoutFactory $resultLayoutFactory,
  65. RawFactory $resultRawFactory,
  66. OrderManagementInterface $orderManagement,
  67. OrderRepositoryInterface $orderRepository,
  68. LoggerInterface $logger,
  69. RegionFactory $regionFactory = null
  70. ) {
  71. $this->regionFactory = $regionFactory ?: ObjectManager::getInstance()->get(RegionFactory::class);
  72. parent::__construct(
  73. $context,
  74. $coreRegistry,
  75. $fileFactory,
  76. $translateInline,
  77. $resultPageFactory,
  78. $resultJsonFactory,
  79. $resultLayoutFactory,
  80. $resultRawFactory,
  81. $orderManagement,
  82. $orderRepository,
  83. $logger
  84. );
  85. }
  86. /**
  87. * Save order address
  88. *
  89. * @return Redirect
  90. */
  91. public function execute()
  92. {
  93. $addressId = $this->getRequest()->getParam('address_id');
  94. /** @var $address OrderAddressInterface|AddressModel */
  95. $address = $this->_objectManager->create(
  96. OrderAddressInterface::class
  97. )->load($addressId);
  98. $data = $this->getRequest()->getPostValue();
  99. $data = $this->updateRegionData($data);
  100. $resultRedirect = $this->resultRedirectFactory->create();
  101. if ($data && $address->getId()) {
  102. $address->addData($data);
  103. try {
  104. $address->save();
  105. $this->_eventManager->dispatch(
  106. 'admin_sales_order_address_update',
  107. [
  108. 'order_id' => $address->getParentId()
  109. ]
  110. );
  111. $this->messageManager->addSuccessMessage(__('You updated the order address.'));
  112. return $resultRedirect->setPath('sales/*/view', ['order_id' => $address->getParentId()]);
  113. } catch (LocalizedException $e) {
  114. $this->messageManager->addErrorMessage($e->getMessage());
  115. } catch (\Exception $e) {
  116. $this->messageManager->addExceptionMessage($e, __('We can\'t update the order address right now.'));
  117. }
  118. return $resultRedirect->setPath('sales/*/address', ['address_id' => $address->getId()]);
  119. } else {
  120. return $resultRedirect->setPath('sales/*/');
  121. }
  122. }
  123. /**
  124. * Update region data
  125. *
  126. * @param array $attributeValues
  127. * @return array
  128. */
  129. private function updateRegionData($attributeValues)
  130. {
  131. if (!empty($attributeValues['region_id'])) {
  132. $newRegion = $this->regionFactory->create()->load($attributeValues['region_id']);
  133. $attributeValues['region_code'] = $newRegion->getCode();
  134. $attributeValues['region'] = $newRegion->getDefaultName();
  135. }
  136. return $attributeValues;
  137. }
  138. }