13
13
use Magento \Customer \Model \Context as CustomerContext ;
14
14
use Magento \Customer \Model \Session as CustomerSession ;
15
15
use Magento \Customer \Model \Url as CustomerUrlManager ;
16
+ use Magento \Eav \Api \AttributeOptionManagementInterface ;
16
17
use Magento \Framework \App \Config \ScopeConfigInterface ;
17
18
use Magento \Framework \App \Http \Context as HttpContext ;
18
19
use Magento \Framework \App \ObjectManager ;
26
27
use Magento \Store \Model \ScopeInterface ;
27
28
28
29
/**
30
+ * Default Config Provider
31
+ *
29
32
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
30
33
* @SuppressWarnings(PHPMD.TooManyFields)
31
34
*/
32
35
class DefaultConfigProvider implements ConfigProviderInterface
33
36
{
37
+ /**
38
+ * @var AttributeOptionManagementInterface
39
+ */
40
+ private $ attributeOptionManager ;
41
+
34
42
/**
35
43
* @var CheckoutHelper
36
44
*/
@@ -194,6 +202,7 @@ class DefaultConfigProvider implements ConfigProviderInterface
194
202
* @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
195
203
* @param UrlInterface $urlBuilder
196
204
* @param AddressMetadataInterface $addressMetadata
205
+ * @param AttributeOptionManagementInterface $attributeOptionManager
197
206
* @codeCoverageIgnore
198
207
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
199
208
*/
@@ -224,7 +233,8 @@ public function __construct(
224
233
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
225
234
\Magento \Quote \Api \PaymentMethodManagementInterface $ paymentMethodManagement ,
226
235
UrlInterface $ urlBuilder ,
227
- AddressMetadataInterface $ addressMetadata = null
236
+ AddressMetadataInterface $ addressMetadata = null ,
237
+ AttributeOptionManagementInterface $ attributeOptionManager = null
228
238
) {
229
239
$ this ->checkoutHelper = $ checkoutHelper ;
230
240
$ this ->checkoutSession = $ checkoutSession ;
@@ -253,10 +263,15 @@ public function __construct(
253
263
$ this ->paymentMethodManagement = $ paymentMethodManagement ;
254
264
$ this ->urlBuilder = $ urlBuilder ;
255
265
$ this ->addressMetadata = $ addressMetadata ?: ObjectManager::getInstance ()->get (AddressMetadataInterface::class);
266
+ $ this ->attributeOptionManager = $ attributeOptionManager ??
267
+ ObjectManager::getInstance ()->get (AttributeOptionManagementInterface::class);
256
268
}
257
269
258
270
/**
259
- * {@inheritdoc}
271
+ * Return configuration array
272
+ *
273
+ * @return array|mixed
274
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
260
275
*/
261
276
public function getConfig ()
262
277
{
@@ -359,7 +374,7 @@ private function filterNotVisibleAttributes(array $attributes)
359
374
}
360
375
}
361
376
362
- return $ attributes ;
377
+ return $ this -> setLabelsToAttributes ( $ attributes) ;
363
378
}
364
379
365
380
/**
@@ -581,6 +596,7 @@ protected function getStaticBaseUrl()
581
596
582
597
/**
583
598
* Return quote totals data
599
+ *
584
600
* @return array
585
601
*/
586
602
private function getTotalsData ()
@@ -612,6 +628,7 @@ private function getTotalsData()
612
628
613
629
/**
614
630
* Returns active carriers codes
631
+ *
615
632
* @return array
616
633
*/
617
634
private function getActiveCarriers ()
@@ -625,6 +642,7 @@ private function getActiveCarriers()
625
642
626
643
/**
627
644
* Returns origin country code
645
+ *
628
646
* @return string
629
647
*/
630
648
private function getOriginCountryCode ()
@@ -638,7 +656,9 @@ private function getOriginCountryCode()
638
656
639
657
/**
640
658
* Returns array of payment methods
641
- * @return array
659
+ *
660
+ * @return array $paymentMethods
661
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
642
662
*/
643
663
private function getPaymentMethods ()
644
664
{
@@ -654,4 +674,59 @@ private function getPaymentMethods()
654
674
}
655
675
return $ paymentMethods ;
656
676
}
677
+
678
+ /**
679
+ * Set Labels to custom Attributes
680
+ *
681
+ * @param array $customAttributes
682
+ * @return array $customAttributes
683
+ * @throws \Magento\Framework\Exception\InputException
684
+ * @throws \Magento\Framework\Exception\StateException
685
+ */
686
+ private function setLabelsToAttributes (array $ customAttributes ) : array
687
+ {
688
+ if (!empty ($ customAttributes )) {
689
+ foreach ($ customAttributes as $ customAttributeCode => $ customAttribute ) {
690
+ $ attributeOptionLabels = $ this ->getAttributeLabels ($ customAttribute , $ customAttributeCode );
691
+ if (!empty ($ attributeOptionLabels )) {
692
+ $ customAttributes [$ customAttributeCode ]['label ' ] = implode (', ' , $ attributeOptionLabels );
693
+ }
694
+ }
695
+ }
696
+
697
+ return $ customAttributes ;
698
+ }
699
+
700
+ /**
701
+ * Get Labels by CustomAttribute and CustomAttributeCode
702
+ *
703
+ * @param array $customAttribute
704
+ * @param string|integer $customAttributeCode
705
+ * @return array $attributeOptionLabels
706
+ * @throws \Magento\Framework\Exception\InputException
707
+ * @throws \Magento\Framework\Exception\StateException
708
+ */
709
+ private function getAttributeLabels (array $ customAttribute , string $ customAttributeCode ) : array
710
+ {
711
+ $ attributeOptionLabels = [];
712
+
713
+ if (!empty ($ customAttribute ['value ' ])) {
714
+ $ customAttributeValues = explode (', ' , $ customAttribute ['value ' ]);
715
+ $ attributeOptions = $ this ->attributeOptionManager ->getItems (
716
+ \Magento \Customer \Model \Indexer \Address \AttributeProvider::ENTITY ,
717
+ $ customAttributeCode
718
+ );
719
+
720
+ if (!empty ($ attributeOptions )) {
721
+ foreach ($ attributeOptions as $ attributeOption ) {
722
+ $ attributeOptionValue = $ attributeOption ->getValue ();
723
+ if (in_array ($ attributeOptionValue , $ customAttributeValues )) {
724
+ $ attributeOptionLabels [] = $ attributeOption ->getLabel () ?? $ attributeOptionValue ;
725
+ }
726
+ }
727
+ }
728
+ }
729
+
730
+ return $ attributeOptionLabels ;
731
+ }
657
732
}
0 commit comments