diff --git a/Plugin/Catalog/Helper/Product/Configuration/AddVisibleInCheckoutAttributesToCustomOptionsPlugin.php b/Plugin/Catalog/Helper/Product/Configuration/AddVisibleInCheckoutAttributesToCustomOptionsPlugin.php index 6106cd9..8e4abcf 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; }