Skip to content

Commit 7cfb7c6

Browse files
committed
Merge remote-tracking branch 'origin/MC-24495' into 2.4-develop-pr117
2 parents 8c974d8 + 88c12e5 commit 7cfb7c6

File tree

5 files changed

+119
-17
lines changed

5 files changed

+119
-17
lines changed

app/code/Magento/Customer/Model/Address/AbstractAddress.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,11 @@ protected function _implodeArrayValues($value)
331331
return '';
332332
}
333333

334-
$isScalar = false;
334+
$isScalar = true;
335335
foreach ($value as $val) {
336-
if (is_scalar($val)) {
337-
$isScalar = true;
336+
if (!is_scalar($val)) {
337+
$isScalar = false;
338+
break;
338339
}
339340
}
340341
if ($isScalar) {

app/code/Magento/Customer/Model/Metadata/Form/File.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public function __construct(
100100
FileProcessorFactory $fileProcessorFactory = null,
101101
IoFile $ioFile = null
102102
) {
103+
$value = $this->prepareFileValue($value);
103104
parent::__construct($localeDate, $logger, $attribute, $localeResolver, $value, $entityTypeCode, $isAjax);
104105
$this->urlEncoder = $urlEncoder;
105106
$this->_fileValidator = $fileValidator;
@@ -302,11 +303,11 @@ public function validateValue($value)
302303
public function compactValue($value)
303304
{
304305
if ($this->getIsAjaxRequest()) {
305-
return $this;
306+
return '';
306307
}
307308

308309
// Remove outdated file (in the case of file uploader UI component)
309-
if (empty($value) && !empty($this->_value)) {
310+
if (!empty($this->_value) && !empty($value['delete'])) {
310311
$this->fileProcessor->removeUploadedFile($this->_value);
311312
return $value;
312313
}
@@ -420,4 +421,19 @@ protected function getFileProcessor()
420421
{
421422
return $this->fileProcessor;
422423
}
424+
425+
/**
426+
* Prepare File value.
427+
*
428+
* @param array|string $value
429+
* @return array|string
430+
*/
431+
private function prepareFileValue($value)
432+
{
433+
if (is_array($value) && isset($value['value'])) {
434+
$value = $value['value'];
435+
}
436+
437+
return $value;
438+
}
423439
}

app/code/Magento/Customer/Test/Unit/Model/Metadata/Form/FileTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ public function testCompactValueIsAjax()
347347
]
348348
);
349349

350-
$this->assertSame($model, $model->compactValue('aValue'));
350+
$this->assertSame('', $model->compactValue('aValue'));
351351
}
352352

353353
public function testCompactValueNoDelete()
@@ -362,12 +362,12 @@ public function testCompactValueNoDelete()
362362
]
363363
);
364364

365-
$this->fileProcessorMock->expects($this->once())
365+
$this->fileProcessorMock->expects($this->any())
366366
->method('removeUploadedFile')
367367
->with('value')
368368
->willReturnSelf();
369369

370-
$this->assertSame([], $model->compactValue([]));
370+
$this->assertSame('value', $model->compactValue([]));
371371
}
372372

373373
public function testCompactValueDelete()
@@ -377,11 +377,11 @@ public function testCompactValueDelete()
377377
$mediaDirMock = $this->getMockForAbstractClass(
378378
\Magento\Framework\Filesystem\Directory\WriteInterface::class
379379
);
380-
$mediaDirMock->expects($this->once())
380+
$mediaDirMock->expects($this->any())
381381
->method('delete')
382382
->with(self::ENTITY_TYPE . '/' . 'value');
383383

384-
$this->fileSystemMock->expects($this->once())
384+
$this->fileSystemMock->expects($this->any())
385385
->method('getDirectoryWrite')
386386
->with(DirectoryList::MEDIA)
387387
->will($this->returnValue($mediaDirMock));
@@ -394,7 +394,7 @@ public function testCompactValueDelete()
394394
]
395395
);
396396

397-
$this->assertSame('', $model->compactValue(['delete' => true]));
397+
$this->assertIsArray($model->compactValue(['delete' => true]));
398398
}
399399

400400
public function testCompactValueTmpFile()
@@ -589,12 +589,12 @@ public function testCompactValueRemoveUiComponentValue()
589589
]
590590
);
591591

592-
$this->fileProcessorMock->expects($this->once())
592+
$this->fileProcessorMock->expects($this->any())
593593
->method('removeUploadedFile')
594594
->with($value)
595595
->willReturnSelf();
596596

597-
$this->assertEquals([], $model->compactValue([]));
597+
$this->assertEquals($value, $model->compactValue([]));
598598
}
599599

600600
public function testCompactValueNoAction()

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

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Magento\Framework\Exception\LocalizedException;
2828
use Magento\Framework\App\ObjectManager;
2929
use Magento\Framework\App\Action\HttpPostActionInterface;
30+
use Magento\Customer\Model\AttributeMetadataDataProvider;
3031

3132
/**
3233
* Sales address save
@@ -52,6 +53,11 @@ class AddressSave extends Order implements HttpPostActionInterface
5253
*/
5354
private $orderAddressRepository;
5455

56+
/**
57+
* @var AttributeMetadataDataProvider
58+
*/
59+
private $attributeMetadataDataProvider;
60+
5561
/**
5662
* @param Context $context
5763
* @param Registry $coreRegistry
@@ -82,7 +88,8 @@ public function __construct(
8288
OrderRepositoryInterface $orderRepository,
8389
LoggerInterface $logger,
8490
RegionFactory $regionFactory = null,
85-
OrderAddressRepositoryInterface $orderAddressRepository = null
91+
OrderAddressRepositoryInterface $orderAddressRepository = null,
92+
AttributeMetadataDataProvider $attributeMetadataDataProvider = null
8693
) {
8794
$this->regionFactory = $regionFactory ?: ObjectManager::getInstance()->get(RegionFactory::class);
8895
$this->orderAddressRepository = $orderAddressRepository ?: ObjectManager::getInstance()
@@ -100,6 +107,8 @@ public function __construct(
100107
$orderRepository,
101108
$logger
102109
);
110+
$this->attributeMetadataDataProvider = $attributeMetadataDataProvider ?: ObjectManager::getInstance()
111+
->get(AttributeMetadataDataProvider::class);
103112
}
104113

105114
/**
@@ -115,6 +124,7 @@ public function execute()
115124
OrderAddressInterface::class
116125
)->load($addressId);
117126
$data = $this->getRequest()->getPostValue();
127+
$data = $this->truncateCustomFileAttributes($data);
118128
$data = $this->updateRegionData($data);
119129
$resultRedirect = $this->resultRedirectFactory->create();
120130
if ($data && $address->getId()) {
@@ -139,7 +149,7 @@ public function execute()
139149
return $resultRedirect->setPath('sales/*/');
140150
}
141151
}
142-
152+
143153
/**
144154
* Update region data
145155
*
@@ -155,4 +165,40 @@ private function updateRegionData($attributeValues)
155165
}
156166
return $attributeValues;
157167
}
168+
169+
/**
170+
* Truncates custom file attributes from a request.
171+
*
172+
* As custom file type attributes are not working workaround is introduced.
173+
*
174+
* @param array $data
175+
* @return array
176+
*/
177+
private function truncateCustomFileAttributes(array $data): array
178+
{
179+
$foundArrays = [];
180+
181+
foreach ($data as $value) {
182+
if (is_array($value)) {
183+
$foundArrays = $value;
184+
}
185+
}
186+
187+
if (empty($foundArrays)) {
188+
return $data;
189+
}
190+
191+
$attributesList = $this->attributeMetadataDataProvider->loadAttributesCollection(
192+
'customer_address',
193+
'adminhtml_customer_address'
194+
);
195+
$attributesList->addFieldToFilter('is_user_defined', 1);
196+
$attributesList->addFieldToFilter('frontend_input', 'file');
197+
198+
foreach ($attributesList as $customFileAttribute) {
199+
unset($data[$customFileAttribute->getAttributeCode()]);
200+
}
201+
202+
return $data;
203+
}
158204
}

app/code/Magento/Sales/Model/AdminOrder/Create.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
namespace Magento\Sales\Model\AdminOrder;
88

99
use Magento\Customer\Api\AddressMetadataInterface;
10+
use Magento\Customer\Api\Data\AttributeMetadataInterface;
1011
use Magento\Customer\Model\Metadata\Form as CustomerForm;
1112
use Magento\Framework\Api\ExtensibleDataObjectConverter;
1213
use Magento\Framework\App\ObjectManager;
1314
use Magento\Quote\Model\Quote\Address;
15+
use Magento\Quote\Model\Quote\Address\CustomAttributeListInterface;
1416
use Magento\Quote\Model\Quote\Item;
1517
use Magento\Sales\Api\Data\OrderAddressInterface;
1618
use Magento\Sales\Model\Order;
@@ -250,6 +252,11 @@ class Create extends \Magento\Framework\DataObject implements \Magento\Checkout\
250252
*/
251253
private $storeManager;
252254

255+
/**
256+
* @var CustomAttributeListInterface
257+
*/
258+
private $customAttributeList;
259+
253260
/**
254261
* @param \Magento\Framework\ObjectManagerInterface $objectManager
255262
* @param \Magento\Framework\Event\ManagerInterface $eventManager
@@ -282,6 +289,7 @@ class Create extends \Magento\Framework\DataObject implements \Magento\Checkout\
282289
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
283290
* @param ExtensibleDataObjectConverter|null $dataObjectConverter
284291
* @param StoreManagerInterface $storeManager
292+
* @param CustomAttributeListInterface|null $customAttributeList
285293
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
286294
*/
287295
public function __construct(
@@ -315,7 +323,8 @@ public function __construct(
315323
array $data = [],
316324
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
317325
ExtensibleDataObjectConverter $dataObjectConverter = null,
318-
StoreManagerInterface $storeManager = null
326+
StoreManagerInterface $storeManager = null,
327+
CustomAttributeListInterface $customAttributeList = null
319328
) {
320329
$this->_objectManager = $objectManager;
321330
$this->_eventManager = $eventManager;
@@ -350,6 +359,8 @@ public function __construct(
350359
$this->dataObjectConverter = $dataObjectConverter ?: ObjectManager::getInstance()
351360
->get(ExtensibleDataObjectConverter::class);
352361
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
362+
$this->customAttributeList = $customAttributeList ?: ObjectManager::getInstance()
363+
->get(CustomAttributeListInterface::class);
353364
}
354365

355366
/**
@@ -1530,7 +1541,8 @@ public function setBillingAddress($address)
15301541
$billingAddress->setData('save_in_address_book', $saveInAddressBook);
15311542

15321543
$quote = $this->getQuote();
1533-
if (!$quote->isVirtual() && $this->getShippingAddress()->getSameAsBilling()) {
1544+
$shippingAddress = $this->getShippingAddress();
1545+
if (!$quote->isVirtual() && $shippingAddress->getSameAsBilling()) {
15341546
$address['save_in_address_book'] = 0;
15351547
$this->setShippingAddress($address);
15361548
}
@@ -1543,9 +1555,36 @@ public function setBillingAddress($address)
15431555
}
15441556
$quote->setBillingAddress($billingAddress);
15451557

1558+
if ($shippingAddress->getSameAsBilling()) {
1559+
$this->synchronizeAddressesFileAttributes();
1560+
}
1561+
15461562
return $this;
15471563
}
15481564

1565+
/**
1566+
* Synchronizes addresses file attributes.
1567+
*
1568+
* @return void
1569+
*/
1570+
private function synchronizeAddressesFileAttributes(): void
1571+
{
1572+
$billingAddress = $this->getBillingAddress();
1573+
$shippingAddress = $this->getShippingAddress();
1574+
1575+
/** @var AttributeMetadataInterface[] $customAttributes */
1576+
$customAttributes = $this->customAttributeList->getAttributes();
1577+
foreach ($customAttributes as $attribute) {
1578+
$attributeCode = $attribute->getAttributeCode();
1579+
if ($attribute->getFrontendInput() === 'file'
1580+
&& !empty($billingAddress->getData($attributeCode))
1581+
&& empty($shippingAddress->getData($attributeCode))
1582+
) {
1583+
$shippingAddress->setData($attributeCode, $billingAddress->getData($attributeCode));
1584+
}
1585+
}
1586+
}
1587+
15491588
/**
15501589
* Set shipping method
15511590
*

0 commit comments

Comments
 (0)