@@ -25,6 +25,131 @@ protected function _addSpecialAttributes(array &$attributes)
25
25
$ attributes ['quote_item_qty ' ] = __ ('Quantity in cart ' );
26
26
$ attributes ['quote_item_price ' ] = __ ('Price in cart ' );
27
27
$ attributes ['quote_item_row_total ' ] = __ ('Row total in cart ' );
28
+
29
+ $ attributes ['parent::category_ids ' ] = __ ('Category (Parent only) ' );
30
+ $ attributes ['children::category_ids ' ] = __ ('Category (Children Only) ' );
31
+ }
32
+
33
+ /**
34
+ * Retrieve attribute
35
+ *
36
+ * @return string
37
+ */
38
+ public function getAttribute ()
39
+ {
40
+ $ attribute = $ this ->getData ('attribute ' );
41
+ if (strpos ($ attribute , ':: ' ) !== false ) {
42
+ list (, $ attribute ) = explode (':: ' , $ attribute );
43
+ }
44
+ return $ attribute ;
45
+ }
46
+
47
+ /**
48
+ * @inheritdoc
49
+ */
50
+ public function getAttributeName ()
51
+ {
52
+ $ attribute = $ this ->getAttribute ();
53
+ if ($ this ->getAttributeScope ()) {
54
+ $ attribute = $ this ->getAttributeScope () . ':: ' . $ attribute ;
55
+ }
56
+ return $ this ->getAttributeOption ($ attribute );
57
+ }
58
+
59
+ /**
60
+ * @inheritdoc
61
+ */
62
+ public function loadAttributeOptions ()
63
+ {
64
+ $ productAttributes = $ this ->_productResource ->loadAllAttributes ()->getAttributesByCode ();
65
+
66
+ $ attributes = [];
67
+ foreach ($ productAttributes as $ attribute ) {
68
+ /* @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
69
+ if (!$ attribute ->isAllowedForRuleCondition () || !$ attribute ->getDataUsingMethod (
70
+ $ this ->_isUsedForRuleProperty
71
+ )
72
+ ) {
73
+ continue ;
74
+ }
75
+ $ frontLabel = $ attribute ->getFrontendLabel ();
76
+ $ attributes [$ attribute ->getAttributeCode ()] = $ frontLabel ;
77
+ $ attributes ['parent:: ' . $ attribute ->getAttributeCode ()] = $ frontLabel . __ ('(Parent Only) ' );
78
+ $ attributes ['children:: ' . $ attribute ->getAttributeCode ()] = $ frontLabel . __ ('(Children Only) ' );
79
+ }
80
+
81
+ $ this ->_addSpecialAttributes ($ attributes );
82
+
83
+ asort ($ attributes );
84
+ $ this ->setAttributeOption ($ attributes );
85
+
86
+ return $ this ;
87
+ }
88
+
89
+ /**
90
+ * @inheritdoc
91
+ */
92
+ public function getAttributeElementHtml ()
93
+ {
94
+ $ html = parent ::getAttributeElementHtml () .
95
+ $ this ->getAttributeScopeElement ()->getHtml ();
96
+ return $ html ;
97
+ }
98
+
99
+ /**
100
+ * Retrieve form element for scope element
101
+ *
102
+ * @return \Magento\Framework\Data\Form\Element\AbstractElement
103
+ */
104
+ private function getAttributeScopeElement ()
105
+ {
106
+ return $ this ->getForm ()->addField (
107
+ $ this ->getPrefix () . '__ ' . $ this ->getId () . '__attribute_scope ' ,
108
+ 'hidden ' ,
109
+ [
110
+ 'name ' => $ this ->elementName . '[ ' . $ this ->getPrefix () . '][ ' . $ this ->getId () . '][attribute_scope] ' ,
111
+ 'value ' => $ this ->getAttributeScope (),
112
+ 'no_span ' => true ,
113
+ 'class ' => 'hidden ' ,
114
+ 'data-form-part ' => $ this ->getFormName ()
115
+ ]
116
+ );
117
+ }
118
+
119
+ /**
120
+ * Set attribute value
121
+ *
122
+ * @param $value
123
+ */
124
+ public function setAttribute ($ value )
125
+ {
126
+ if (strpos ($ value , ':: ' ) !== false ) {
127
+ list ($ scope , $ attribute ) = explode (':: ' , $ value );
128
+ $ this ->setData ('attribute_scope ' , $ scope );
129
+ $ this ->setData ('attribute ' , $ attribute );
130
+ } else {
131
+ $ this ->setData ('attribute ' , $ value );
132
+ }
133
+ }
134
+
135
+ /**
136
+ * @inheritdoc
137
+ */
138
+ public function loadArray ($ arr )
139
+ {
140
+ parent ::loadArray ($ arr );
141
+ $ this ->setAttributeScope (isset ($ arr ['attribute_scope ' ]) ? $ arr ['attribute_scope ' ] : null );
142
+ return $ this ;
143
+ }
144
+
145
+ /**
146
+ * @inheritdoc
147
+ */
148
+ public function asArray (array $ arrAttributes = [])
149
+ {
150
+ $ out = parent ::asArray ($ arrAttributes );
151
+ $ out ['attribute_scope ' ] = $ this ->getAttributeScope ();
152
+ return $ out ;
28
153
}
29
154
30
155
/**
0 commit comments