Skip to content

Commit 92e34d7

Browse files
Oleksandr_HodzevychOleksandr_Hodzevych
authored andcommitted
MAGETWO-91760: Custom address attributes displays with wrong value on checkout
- Fixed add label for custom attributes
1 parent 0e2bca6 commit 92e34d7

File tree

7 files changed

+608
-2
lines changed

7 files changed

+608
-2
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Checkout\Plugin\Block;
9+
10+
use Magento\Checkout\Block\Cart\Shipping;
11+
use Magento\Checkout\Block\Onepage;
12+
13+
/**
14+
* Class AbstractResetCheckoutConfig
15+
* Needed for reformat Customer Data address with custom attributes as options add labels for correct view on UI
16+
*/
17+
class AbstractResetCheckoutConfig
18+
{
19+
/**
20+
* @var \Magento\Eav\Api\AttributeOptionManagementInterface
21+
*/
22+
private $attributeOptionManager;
23+
24+
/*
25+
* @var \Magento\Framework\Json\Helper\Data
26+
*/
27+
private $serializer;
28+
29+
/**
30+
* @param \Magento\Eav\Api\AttributeOptionManagementInterface $attributeOptionManager
31+
* @param \Magento\Framework\Serialize\SerializerInterface
32+
*/
33+
public function __construct(
34+
\Magento\Eav\Api\AttributeOptionManagementInterface $attributeOptionManager,
35+
\Magento\Framework\Serialize\SerializerInterface $serializer
36+
)
37+
{
38+
$this->attributeOptionManager = $attributeOptionManager;
39+
$this->serializer = $serializer;
40+
}
41+
42+
/**
43+
* After Get Checkout Config
44+
*
45+
* @param Onepage|Shipping $subject
46+
* @param mixed $result
47+
* @return string
48+
* @throws \Magento\Framework\Exception\InputException
49+
* @throws \Magento\Framework\Exception\StateException
50+
*/
51+
protected function getSerializedCheckoutConfig($subject, $result)
52+
{
53+
$resultArray = $data = $this->serializer->unserialize($result);
54+
$customerAddresses = $resultArray['customerData']['addresses'];
55+
$hasAtLeastOneOptionAttribute = false;
56+
57+
if (is_array($customerAddresses) && !empty($customerAddresses)) {
58+
foreach ($customerAddresses as $customerAddressIndex => $customerAddress) {
59+
if (!empty($customerAddress['custom_attributes'])) {
60+
foreach ($customerAddress['custom_attributes'] as $customAttributeCode => $customAttribute) {
61+
$attributeOptionLabels = $this->getAttributeLabels($customAttribute, $customAttributeCode);
62+
63+
if (!empty($attributeOptionLabels)) {
64+
$hasAtLeastOneOptionAttribute = true;
65+
$resultArray['customerData']['addresses'][$customerAddressIndex]['custom_attributes']
66+
[$customAttributeCode]['label'] = implode(', ', $attributeOptionLabels);
67+
}
68+
}
69+
}
70+
}
71+
}
72+
73+
return $hasAtLeastOneOptionAttribute ? $this->serializer->serialize($resultArray) : $result;
74+
}
75+
76+
/**
77+
* Get Labels by CustomAttribute and CustomAttributeCode
78+
*
79+
* @param $customAttribute
80+
* @param $customAttributeCode
81+
* @return array
82+
* @throws \Magento\Framework\Exception\InputException
83+
* @throws \Magento\Framework\Exception\StateException
84+
*/
85+
private function getAttributeLabels($customAttribute, $customAttributeCode)
86+
{
87+
$attributeOptionLabels = [];
88+
$customAttributeValues = explode(',', $customAttribute['value']);
89+
$attributeOptions = $this->attributeOptionManager->getItems(
90+
\Magento\Customer\Model\Indexer\Address\AttributeProvider::ENTITY,
91+
$customAttributeCode
92+
);
93+
94+
if (!empty($attributeOptions)) {
95+
foreach ($attributeOptions as $attributeOption) {
96+
$attributeOptionValue = $attributeOption->getValue();
97+
if (in_array($attributeOptionValue, $customAttributeValues)) {
98+
$attributeOptionLabels[] = $attributeOption->getLabel() ?? $attributeOptionValue;
99+
}
100+
}
101+
}
102+
103+
return $attributeOptionLabels;
104+
}
105+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Checkout\Plugin\Block\Cart;
9+
10+
use Magento\Checkout\Block\Cart\Shipping;
11+
use Magento\Checkout\Plugin\Block\AbstractResetCheckoutConfig;
12+
13+
/**
14+
* Class ResetCheckoutConfigOnCartShipping
15+
* Needed for reformat Customer Data address with custom attributes as options add labels for correct view on ShippingUI
16+
*/
17+
class ResetCheckoutConfigOnCartShipping extends AbstractResetCheckoutConfig
18+
{
19+
/**
20+
* After Get Checkout Config
21+
*
22+
* @param Shipping $subject
23+
* @param mixed $result
24+
* @return string
25+
* @throws \Magento\Framework\Exception\InputException
26+
* @throws \Magento\Framework\Exception\StateException
27+
*/
28+
public function afterGetSerializedCheckoutConfig(Shipping $subject, $result)
29+
{
30+
return $this->getSerializedCheckoutConfig($subject, $result);
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Checkout\Plugin\Block;
9+
10+
use Magento\Checkout\Block\Onepage;
11+
12+
/**
13+
* Class ResetCheckoutConfigOnOnePage
14+
* Needed for reformat Customer Data address with custom attributes as options add labels for correct view on UI OnePage
15+
*/
16+
class ResetCheckoutConfigOnOnePage extends AbstractResetCheckoutConfig
17+
{
18+
/**
19+
* After Get Checkout Config
20+
*
21+
* @param Onepage $subject
22+
* @param mixed $result
23+
* @return string
24+
* @throws \Magento\Framework\Exception\InputException
25+
* @throws \Magento\Framework\Exception\StateException
26+
*/
27+
public function afterGetSerializedCheckoutConfig(Onepage $subject, $result)
28+
{
29+
return $this->getSerializedCheckoutConfig($subject, $result);
30+
}
31+
}

0 commit comments

Comments
 (0)