From 4b9da553cee786f6612505d5fc50dcae95c8f18f Mon Sep 17 00:00:00 2001 From: Frederik Rommel Date: Tue, 11 Oct 2022 18:13:36 +0200 Subject: [PATCH 1/2] [FEATURE] display attribute value of child product in cart --- ...heckoutAttributesToCustomOptionsPlugin.php | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Plugin/Catalog/Helper/Product/Configuration/AddVisibleInCheckoutAttributesToCustomOptionsPlugin.php b/Plugin/Catalog/Helper/Product/Configuration/AddVisibleInCheckoutAttributesToCustomOptionsPlugin.php index 6106cd9..c08a140 100644 --- a/Plugin/Catalog/Helper/Product/Configuration/AddVisibleInCheckoutAttributesToCustomOptionsPlugin.php +++ b/Plugin/Catalog/Helper/Product/Configuration/AddVisibleInCheckoutAttributesToCustomOptionsPlugin.php @@ -9,6 +9,7 @@ use FireGento\MageSetup\Service\GetVisibleCheckoutAttributesServiceInterface; use Magento\Catalog\Helper\Product\Configuration; use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface; +use Magento\Quote\Model\Quote\Item\AbstractItem; /** * Plugin to add visible in checkout attributes to the custom option list. @@ -42,8 +43,34 @@ public function __construct(GetVisibleCheckoutAttributesServiceInterface $getVis public function afterGetCustomOptions(Configuration $configuration, array $customOptions, ItemInterface $item) { $attributes = $this->getVisibleCheckoutAttributesService->execute(); + + $configurableAttributes = $item->getOptionByCode('attributes') ? $item->getOptionByCode('attributes')->getValue() : []; + $configurableAttributes = $configurableAttributes ? array_keys(json_decode($configurableAttributes, true)) : []; + + $product = $item->getProduct(); + + if (!$product) { + return []; + } + foreach ($attributes as $attribute) { - $value = $attribute->getFrontend()->getValue($item->getProduct()); + $attributeFrontend = $attribute->getFrontend(); + $value = $attributeFrontend->getValue($product); + + if ($item instanceof AbstractItem && $product->getTypeId() == 'configurable') { + if (in_array($attribute->getId(), $configurableAttributes) || !count($item->getChildren())) { + // attribute is a configurable attribute. Magento will print it separately + // or item has no children (but this should never occur) + continue; + } + + $children = $item->getChildren(); + if ($children[0] instanceof AbstractItem && $children[0]->getProduct()) { + // fetch the attribute value of the child + $value = $attributeFrontend->getValue($children[0]->getProduct()) ?: $value; + } + } + if (!$value) { continue; } From ec62709bf0dd7941d3b561091a666f298e9f2563 Mon Sep 17 00:00:00 2001 From: Roman Hutterer Date: Sat, 9 Dec 2023 17:23:04 +0100 Subject: [PATCH 2/2] Update Plugin/Catalog/Helper/Product/Configuration/AddVisibleInCheckoutAttributesToCustomOptionsPlugin.php Make comparison stricter Co-authored-by: Simon Sprankel --- .../AddVisibleInCheckoutAttributesToCustomOptionsPlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugin/Catalog/Helper/Product/Configuration/AddVisibleInCheckoutAttributesToCustomOptionsPlugin.php b/Plugin/Catalog/Helper/Product/Configuration/AddVisibleInCheckoutAttributesToCustomOptionsPlugin.php index c08a140..8e4abcf 100644 --- a/Plugin/Catalog/Helper/Product/Configuration/AddVisibleInCheckoutAttributesToCustomOptionsPlugin.php +++ b/Plugin/Catalog/Helper/Product/Configuration/AddVisibleInCheckoutAttributesToCustomOptionsPlugin.php @@ -57,7 +57,7 @@ public function afterGetCustomOptions(Configuration $configuration, array $custo $attributeFrontend = $attribute->getFrontend(); $value = $attributeFrontend->getValue($product); - if ($item instanceof AbstractItem && $product->getTypeId() == 'configurable') { + if ($item instanceof AbstractItem && $product->getTypeId() === 'configurable') { if (in_array($attribute->getId(), $configurableAttributes) || !count($item->getChildren())) { // attribute is a configurable attribute. Magento will print it separately // or item has no children (but this should never occur)