11
11
use Magento \Customer \Api \CustomerRepositoryInterface ;
12
12
use Magento \Customer \Api \Data \CustomerInterface ;
13
13
use Magento \Customer \Model \ResourceModel \Customer as CustomerResource ;
14
+ use Magento \Eav \Api \AttributeRepositoryInterface ;
14
15
use Magento \Framework \Api \SearchCriteriaBuilder ;
15
16
use Magento \Framework \Api \SearchCriteriaInterface ;
16
17
use Magento \Framework \Exception \LocalizedException ;
@@ -61,8 +62,18 @@ class Profiles
61
62
* @var LogRepository
62
63
*/
63
64
private $ logRepository ;
64
-
65
+ /**
66
+ * @var GroupCollectionFactory
67
+ */
65
68
private $ groupCollectionFactory ;
69
+ /**
70
+ * @var AttributeRepositoryInterface
71
+ */
72
+ private $ attributeRepository ;
73
+ /**
74
+ * @var array
75
+ */
76
+ private $ attributeSourceMap = [];
66
77
67
78
/**
68
79
* Profiles constructor.
@@ -73,6 +84,9 @@ class Profiles
73
84
* @param SearchCriteriaBuilder $searchCriteriaBuilder
74
85
* @param CustomerResource $customerResource
75
86
* @param AddressRepository $addressRepository
87
+ * @param LogRepository $logRepository
88
+ * @param GroupCollectionFactory $groupCollectionFactory
89
+ * @param AttributeRepositoryInterface $attributeRepository
76
90
*/
77
91
public function __construct (
78
92
Subscriber $ subscriber ,
@@ -82,7 +96,8 @@ public function __construct(
82
96
CustomerResource $ customerResource ,
83
97
AddressRepository $ addressRepository ,
84
98
LogRepository $ logRepository ,
85
- GroupCollectionFactory $ groupCollectionFactory
99
+ GroupCollectionFactory $ groupCollectionFactory ,
100
+ AttributeRepositoryInterface $ attributeRepository
86
101
) {
87
102
$ this ->subscriber = $ subscriber ;
88
103
$ this ->storeManager = $ storeManager ;
@@ -92,6 +107,7 @@ public function __construct(
92
107
$ this ->addressRepository = $ addressRepository ;
93
108
$ this ->logRepository = $ logRepository ;
94
109
$ this ->groupCollectionFactory = $ groupCollectionFactory ;
110
+ $ this ->attributeRepository = $ attributeRepository ;
95
111
}
96
112
97
113
/**
@@ -123,7 +139,8 @@ public function execute(int $storeId, array $extra = [], SearchCriteriaInterface
123
139
124
140
$ customAttributes = $ customer ->getCustomAttributes ();
125
141
foreach ($ customAttributes as $ attributeCode => $ attribute ) {
126
- $ mainData ['eav_ ' . $ attributeCode ] = $ attribute ->getValue ();
142
+ $ type = $ this ->getAttributeType ($ attributeCode );
143
+ $ mainData ['eav_ ' . $ attributeCode ] = $ this ->getAttributeValue ($ attribute , $ type );
127
144
}
128
145
129
146
if ($ billingId = $ customer ->getDefaultBilling ()) {
@@ -237,4 +254,67 @@ private function getCustomersGroups(): array
237
254
}
238
255
return $ customerGroups ;
239
256
}
240
- }
257
+
258
+ /**
259
+ * @param $attribute
260
+ * @return string
261
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
262
+ */
263
+ private function getAttributeType ($ attribute ): string
264
+ {
265
+ return $ this ->attributeRepository ->get ('customer ' , $ attribute )
266
+ ->getFrontendInput ();
267
+ }
268
+
269
+ /**
270
+ * @param $attribute
271
+ * @return mixed|string
272
+ */
273
+ private function getAttributeValue ($ attribute , $ type )
274
+ {
275
+ $ value = '' ;
276
+ if (!$ attribute || !$ type ) {
277
+ return $ value ;
278
+ }
279
+ if ($ type == 'select ' || $ type == 'multiselect ' ) {
280
+ try {
281
+ $ attributeCode = $ attribute ->getAttributeCode ();
282
+ $ attributeSource = $ this ->getAttributeSource ($ attributeCode );
283
+ $ attributeValue = $ attribute ->getValue ();
284
+
285
+ if ($ type == 'multiselect ' ) {
286
+ $ value = [];
287
+ $ values = explode (', ' , $ attributeValue );
288
+ foreach ($ values as $ singleValue ) {
289
+ $ label = $ attributeSource ->getOptionText ($ singleValue );
290
+ if ($ label ) {
291
+ $ value [] = $ label ;
292
+ }
293
+ }
294
+ $ value = implode (', ' , $ value );
295
+ } else {
296
+ $ value = $ attributeSource ->getOptionText ($ attributeValue );
297
+ }
298
+ } catch (\Exception $ e ) {
299
+ $ value = 'Error retrieving value ' ;
300
+ }
301
+ } else {
302
+ $ value = $ attribute ->getValue ();
303
+ }
304
+ return $ value ;
305
+ }
306
+
307
+ /**
308
+ * @param $attributeCode
309
+ * @return mixed
310
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
311
+ */
312
+ private function getAttributeSource ($ attributeCode )
313
+ {
314
+ if (!isset ($ this ->attributeSourceMap [$ attributeCode ])) {
315
+ $ this ->attributeSourceMap [$ attributeCode ] =
316
+ $ this ->attributeRepository ->get ('customer ' , $ attributeCode )->getSource ();
317
+ }
318
+ return $ this ->attributeSourceMap [$ attributeCode ];
319
+ }
320
+ }
0 commit comments