Skip to content

Commit 836681b

Browse files
committed
MAGETWO-91760: Custom address attributes displays with wrong value on checkout
- Fix CR comments - Fix static tests
1 parent c3d95bd commit 836681b

File tree

9 files changed

+167
-679
lines changed

9 files changed

+167
-679
lines changed

app/code/Magento/Checkout/Model/DefaultConfigProvider.php

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Customer\Model\Context as CustomerContext;
1414
use Magento\Customer\Model\Session as CustomerSession;
1515
use Magento\Customer\Model\Url as CustomerUrlManager;
16+
use Magento\Eav\Api\AttributeOptionManagementInterface;
1617
use Magento\Framework\App\Config\ScopeConfigInterface;
1718
use Magento\Framework\App\Http\Context as HttpContext;
1819
use Magento\Framework\App\ObjectManager;
@@ -26,11 +27,18 @@
2627
use Magento\Store\Model\ScopeInterface;
2728

2829
/**
30+
* Default Config Provider
31+
*
2932
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3033
* @SuppressWarnings(PHPMD.TooManyFields)
3134
*/
3235
class DefaultConfigProvider implements ConfigProviderInterface
3336
{
37+
/**
38+
* @var AttributeOptionManagementInterface
39+
*/
40+
private $attributeOptionManager;
41+
3442
/**
3543
* @var CheckoutHelper
3644
*/
@@ -194,6 +202,7 @@ class DefaultConfigProvider implements ConfigProviderInterface
194202
* @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
195203
* @param UrlInterface $urlBuilder
196204
* @param AddressMetadataInterface $addressMetadata
205+
* @param AttributeOptionManagementInterface $attributeOptionManager
197206
* @codeCoverageIgnore
198207
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
199208
*/
@@ -224,7 +233,8 @@ public function __construct(
224233
\Magento\Store\Model\StoreManagerInterface $storeManager,
225234
\Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement,
226235
UrlInterface $urlBuilder,
227-
AddressMetadataInterface $addressMetadata = null
236+
AddressMetadataInterface $addressMetadata = null,
237+
AttributeOptionManagementInterface $attributeOptionManager = null
228238
) {
229239
$this->checkoutHelper = $checkoutHelper;
230240
$this->checkoutSession = $checkoutSession;
@@ -253,10 +263,15 @@ public function __construct(
253263
$this->paymentMethodManagement = $paymentMethodManagement;
254264
$this->urlBuilder = $urlBuilder;
255265
$this->addressMetadata = $addressMetadata ?: ObjectManager::getInstance()->get(AddressMetadataInterface::class);
266+
$this->attributeOptionManager = $attributeOptionManager ??
267+
ObjectManager::getInstance()->get(AttributeOptionManagementInterface::class);
256268
}
257269

258270
/**
259-
* {@inheritdoc}
271+
* Return configuration array
272+
*
273+
* @return array|mixed
274+
* @throws \Magento\Framework\Exception\NoSuchEntityException
260275
*/
261276
public function getConfig()
262277
{
@@ -359,7 +374,7 @@ private function filterNotVisibleAttributes(array $attributes)
359374
}
360375
}
361376

362-
return $attributes;
377+
return $this->setLabelsToAttributes($attributes);
363378
}
364379

365380
/**
@@ -581,6 +596,7 @@ protected function getStaticBaseUrl()
581596

582597
/**
583598
* Return quote totals data
599+
*
584600
* @return array
585601
*/
586602
private function getTotalsData()
@@ -612,6 +628,7 @@ private function getTotalsData()
612628

613629
/**
614630
* Returns active carriers codes
631+
*
615632
* @return array
616633
*/
617634
private function getActiveCarriers()
@@ -625,6 +642,7 @@ private function getActiveCarriers()
625642

626643
/**
627644
* Returns origin country code
645+
*
628646
* @return string
629647
*/
630648
private function getOriginCountryCode()
@@ -638,7 +656,9 @@ private function getOriginCountryCode()
638656

639657
/**
640658
* Returns array of payment methods
641-
* @return array
659+
*
660+
* @return array $paymentMethods
661+
* @throws \Magento\Framework\Exception\NoSuchEntityException
642662
*/
643663
private function getPaymentMethods()
644664
{
@@ -654,4 +674,59 @@ private function getPaymentMethods()
654674
}
655675
return $paymentMethods;
656676
}
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+
}
657732
}

app/code/Magento/Checkout/Plugin/Block/AbstractResetCheckoutConfig.php

Lines changed: 0 additions & 106 deletions
This file was deleted.

app/code/Magento/Checkout/Plugin/Block/Cart/ResetCheckoutConfigOnCartShipping.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

app/code/Magento/Checkout/Plugin/Block/ResetCheckoutConfigOnOnePage.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)