|
| 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 | +} |
0 commit comments