|
6 | 6 |
|
7 | 7 | namespace Magento\Sales\Model\Order;
|
8 | 8 |
|
| 9 | +use Magento\Customer\Model\AttributeMetadataDataProvider; |
| 10 | +use Magento\Customer\Model\ResourceModel\Form\Attribute\Collection as AttributeCollection; |
9 | 11 | use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
|
| 12 | +use Magento\Framework\App\ObjectManager; |
| 13 | +use Magento\Sales\Api\Data\OrderAddressInterface; |
10 | 14 | use Magento\Sales\Model\ResourceModel\Metadata;
|
11 | 15 | use Magento\Sales\Api\Data\OrderAddressSearchResultInterfaceFactory as SearchResultFactory;
|
12 | 16 | use Magento\Framework\Exception\CouldNotDeleteException;
|
@@ -41,20 +45,88 @@ class AddressRepository implements \Magento\Sales\Api\OrderAddressRepositoryInte
|
41 | 45 | */
|
42 | 46 | private $collectionProcessor;
|
43 | 47 |
|
| 48 | + /** |
| 49 | + * @var AttributeMetadataDataProvider |
| 50 | + */ |
| 51 | + private $attributeMetadataDataProvider; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var AttributeCollection|null |
| 55 | + */ |
| 56 | + private $attributesList = null; |
| 57 | + |
44 | 58 | /**
|
45 | 59 | * AddressRepository constructor.
|
46 | 60 | * @param Metadata $metadata
|
47 | 61 | * @param SearchResultFactory $searchResultFactory
|
48 | 62 | * @param CollectionProcessorInterface|null $collectionProcessor
|
| 63 | + * @param AttributeMetadataDataProvider $attributeMetadataDataProvider |
49 | 64 | */
|
50 | 65 | public function __construct(
|
51 | 66 | Metadata $metadata,
|
52 | 67 | SearchResultFactory $searchResultFactory,
|
53 |
| - CollectionProcessorInterface $collectionProcessor = null |
| 68 | + CollectionProcessorInterface $collectionProcessor = null, |
| 69 | + AttributeMetadataDataProvider $attributeMetadataDataProvider = null |
54 | 70 | ) {
|
55 | 71 | $this->metadata = $metadata;
|
56 | 72 | $this->searchResultFactory = $searchResultFactory;
|
57 | 73 | $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; |
58 | 130 | }
|
59 | 131 |
|
60 | 132 | /**
|
@@ -98,7 +170,7 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
|
98 | 170 | $searchResult = $this->searchResultFactory->create();
|
99 | 171 | $this->collectionProcessor->process($searchCriteria, $searchResult);
|
100 | 172 | $searchResult->setSearchCriteria($searchCriteria);
|
101 |
| - |
| 173 | + |
102 | 174 | return $searchResult;
|
103 | 175 | }
|
104 | 176 |
|
@@ -144,6 +216,7 @@ public function deleteById($id)
|
144 | 216 | */
|
145 | 217 | public function save(\Magento\Sales\Api\Data\OrderAddressInterface $entity)
|
146 | 218 | {
|
| 219 | + $this->formatCustomAddressAttributes($entity); |
147 | 220 | try {
|
148 | 221 | $this->metadata->getMapper()->save($entity);
|
149 | 222 | $this->registry[$entity->getEntityId()] = $entity;
|
|
0 commit comments