Skip to content

Commit ff022bd

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-99364' into 2.3-develop-pr22
2 parents 9890a61 + 1ec59c2 commit ff022bd

File tree

6 files changed

+525
-81
lines changed

6 files changed

+525
-81
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Backend\App\Action\Context;
1111
use Magento\Backend\Model\View\Result\Redirect;
1212
use Magento\Directory\Model\RegionFactory;
13+
use Magento\Sales\Api\OrderAddressRepositoryInterface;
1314
use Magento\Sales\Api\OrderManagementInterface;
1415
use Magento\Sales\Api\OrderRepositoryInterface;
1516
use Magento\Sales\Api\Data\OrderAddressInterface;
@@ -25,11 +26,14 @@
2526
use Magento\Framework\Controller\Result\RawFactory;
2627
use Magento\Framework\Exception\LocalizedException;
2728
use Magento\Framework\App\ObjectManager;
29+
use Magento\Framework\App\Action\HttpPostActionInterface;
2830

2931
/**
32+
* Sales address save
33+
*
3034
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3135
*/
32-
class AddressSave extends Order
36+
class AddressSave extends Order implements HttpPostActionInterface
3337
{
3438
/**
3539
* Authorization level of a basic admin session
@@ -43,6 +47,11 @@ class AddressSave extends Order
4347
*/
4448
private $regionFactory;
4549

50+
/**
51+
* @var OrderAddressRepositoryInterface
52+
*/
53+
private $orderAddressRepository;
54+
4655
/**
4756
* @param Context $context
4857
* @param Registry $coreRegistry
@@ -56,6 +65,7 @@ class AddressSave extends Order
5665
* @param OrderRepositoryInterface $orderRepository
5766
* @param LoggerInterface $logger
5867
* @param RegionFactory|null $regionFactory
68+
* @param OrderAddressRepositoryInterface|null $orderAddressRepository
5969
*
6070
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6171
*/
@@ -71,9 +81,12 @@ public function __construct(
7181
OrderManagementInterface $orderManagement,
7282
OrderRepositoryInterface $orderRepository,
7383
LoggerInterface $logger,
74-
RegionFactory $regionFactory = null
84+
RegionFactory $regionFactory = null,
85+
OrderAddressRepositoryInterface $orderAddressRepository = null
7586
) {
7687
$this->regionFactory = $regionFactory ?: ObjectManager::getInstance()->get(RegionFactory::class);
88+
$this->orderAddressRepository = $orderAddressRepository ?: ObjectManager::getInstance()
89+
->get(OrderAddressRepositoryInterface::class);
7790
parent::__construct(
7891
$context,
7992
$coreRegistry,
@@ -107,7 +120,7 @@ public function execute()
107120
if ($data && $address->getId()) {
108121
$address->addData($data);
109122
try {
110-
$address->save();
123+
$this->orderAddressRepository->save($address);
111124
$this->_eventManager->dispatch(
112125
'admin_sales_order_address_update',
113126
[

app/code/Magento/Sales/Model/Order/AddressRepository.php

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
namespace Magento\Sales\Model\Order;
88

9+
use Magento\Customer\Model\AttributeMetadataDataProvider;
10+
use Magento\Customer\Model\ResourceModel\Form\Attribute\Collection as AttributeCollection;
911
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Sales\Api\Data\OrderAddressInterface;
1014
use Magento\Sales\Model\ResourceModel\Metadata;
1115
use Magento\Sales\Api\Data\OrderAddressSearchResultInterfaceFactory as SearchResultFactory;
1216
use Magento\Framework\Exception\CouldNotDeleteException;
@@ -41,20 +45,88 @@ class AddressRepository implements \Magento\Sales\Api\OrderAddressRepositoryInte
4145
*/
4246
private $collectionProcessor;
4347

48+
/**
49+
* @var AttributeMetadataDataProvider
50+
*/
51+
private $attributeMetadataDataProvider;
52+
53+
/**
54+
* @var AttributeCollection|null
55+
*/
56+
private $attributesList = null;
57+
4458
/**
4559
* AddressRepository constructor.
4660
* @param Metadata $metadata
4761
* @param SearchResultFactory $searchResultFactory
4862
* @param CollectionProcessorInterface|null $collectionProcessor
63+
* @param AttributeMetadataDataProvider $attributeMetadataDataProvider
4964
*/
5065
public function __construct(
5166
Metadata $metadata,
5267
SearchResultFactory $searchResultFactory,
53-
CollectionProcessorInterface $collectionProcessor = null
68+
CollectionProcessorInterface $collectionProcessor = null,
69+
AttributeMetadataDataProvider $attributeMetadataDataProvider = null
5470
) {
5571
$this->metadata = $metadata;
5672
$this->searchResultFactory = $searchResultFactory;
5773
$this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
74+
$this->attributeMetadataDataProvider = $attributeMetadataDataProvider ?: ObjectManager::getInstance()
75+
->get(AttributeMetadataDataProvider::class);
76+
}
77+
78+
/**
79+
* Format multiline and multiselect attributes
80+
*
81+
* @param OrderAddressInterface $orderAddress
82+
*
83+
* @return void
84+
*/
85+
private function formatCustomAddressAttributes(OrderAddressInterface $orderAddress)
86+
{
87+
$attributesList = $this->getAttributesList();
88+
89+
foreach ($attributesList as $attribute) {
90+
$attributeCode = $attribute->getAttributeCode();
91+
if (!$orderAddress->hasData($attributeCode)) {
92+
continue;
93+
}
94+
$attributeValue = $orderAddress->getData($attributeCode);
95+
if (is_array($attributeValue)) {
96+
$glue = $attribute->getFrontendInput() === 'multiline' ? PHP_EOL : ',';
97+
$attributeValue = trim(implode($glue, $attributeValue));
98+
}
99+
$orderAddress->setData($attributeCode, $attributeValue);
100+
}
101+
}
102+
103+
/**
104+
* Get list of custom attributes.
105+
*
106+
* @return AttributeCollection|null
107+
*/
108+
private function getAttributesList()
109+
{
110+
if (!$this->attributesList) {
111+
$attributesList = $this->attributeMetadataDataProvider->loadAttributesCollection(
112+
'customer_address',
113+
'customer_register_address'
114+
);
115+
$attributesList->addFieldToFilter('is_user_defined', 1);
116+
$attributesList->addFieldToFilter(
117+
'frontend_input',
118+
[
119+
'in' => [
120+
'multiline',
121+
'multiselect',
122+
],
123+
]
124+
);
125+
126+
$this->attributesList = $attributesList;
127+
}
128+
129+
return $this->attributesList;
58130
}
59131

60132
/**
@@ -98,7 +170,7 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
98170
$searchResult = $this->searchResultFactory->create();
99171
$this->collectionProcessor->process($searchCriteria, $searchResult);
100172
$searchResult->setSearchCriteria($searchCriteria);
101-
173+
102174
return $searchResult;
103175
}
104176

@@ -144,6 +216,7 @@ public function deleteById($id)
144216
*/
145217
public function save(\Magento\Sales\Api\Data\OrderAddressInterface $entity)
146218
{
219+
$this->formatCustomAddressAttributes($entity);
147220
try {
148221
$this->metadata->getMapper()->save($entity);
149222
$this->registry[$entity->getEntityId()] = $entity;

0 commit comments

Comments
 (0)