99use FireGento \MageSetup \Service \GetVisibleCheckoutAttributesServiceInterface ;
1010use Magento \Catalog \Helper \Product \Configuration ;
1111use Magento \Catalog \Model \Product \Configuration \Item \ItemInterface ;
12+ use Magento \Framework \Serialize \Serializer \Json ;
13+ use Magento \Quote \Model \Quote \Item \AbstractItem ;
1214
1315/**
1416 * Plugin to add visible in checkout attributes to the custom option list.
@@ -20,14 +22,23 @@ class AddVisibleInCheckoutAttributesToCustomOptionsPlugin
2022 */
2123 private $ getVisibleCheckoutAttributesService ;
2224
25+ /**
26+ * @var Json
27+ */
28+ private $ json ;
29+
2330 /**
2431 * AroundGetCustomOptionsPlugin constructor.
2532 *
2633 * @param GetVisibleCheckoutAttributesServiceInterface $getVisibleCheckoutAttributesService
34+ * @param Json $json
2735 */
28- public function __construct (GetVisibleCheckoutAttributesServiceInterface $ getVisibleCheckoutAttributesService )
29- {
36+ public function __construct (
37+ GetVisibleCheckoutAttributesServiceInterface $ getVisibleCheckoutAttributesService ,
38+ Json $ json
39+ ) {
3040 $ this ->getVisibleCheckoutAttributesService = $ getVisibleCheckoutAttributesService ;
41+ $ this ->json = $ json ;
3142 }
3243
3344 /**
@@ -41,12 +52,40 @@ public function __construct(GetVisibleCheckoutAttributesServiceInterface $getVis
4152 */
4253 public function afterGetCustomOptions (Configuration $ configuration , array $ customOptions , ItemInterface $ item )
4354 {
55+ $ product = $ item ->getProduct ();
56+
57+ if (!$ product ) {
58+ return [];
59+ }
60+
61+ $ configurableAttributes = $ item ->getOptionByCode ('attributes ' )
62+ ? $ item ->getOptionByCode ('attributes ' )->getValue ()
63+ : [];
64+ $ configurableAttributes = is_string ($ configurableAttributes )
65+ ? array_keys ($ this ->json ->unserialize ($ configurableAttributes ) ?: [])
66+ : [];
67+
4468 $ attributes = $ this ->getVisibleCheckoutAttributesService ->execute ();
4569 foreach ($ attributes as $ attributeCode => $ attribute ) {
70+ $ attributeFrontend = $ attribute ->getFrontend ();
4671 if ($ attributeCode === 'sku ' ) {
47- $ value = $ item -> getProduct () ->getSku ();
72+ $ value = $ product ->getSku ();
4873 } else {
49- $ value = $ attribute ->getFrontend ()->getValue ($ item ->getProduct ());
74+ $ value = $ attributeFrontend ->getValue ($ product );
75+ }
76+
77+ if ($ item instanceof AbstractItem && $ product ->getTypeId () === 'configurable ' ) {
78+ if (in_array ($ attribute ->getId (), $ configurableAttributes ) || !count ($ item ->getChildren ())) {
79+ // attribute is a configurable attribute. Magento will output it separately
80+ // or item has no children (but this should never occur)
81+ continue ;
82+ }
83+
84+ $ children = $ item ->getChildren ();
85+ if ($ children [0 ] instanceof AbstractItem && $ children [0 ]->getProduct ()) {
86+ // fetch the attribute value of the child
87+ $ value = $ attributeFrontend ->getValue ($ children [0 ]->getProduct ()) ?: $ value ;
88+ }
5089 }
5190
5291 if (!$ value ) {
0 commit comments