11
11
12
12
use Magento \Catalog \Api \Data \ProductInterface ;
13
13
use Magento \Catalog \Model \ProductCategoryList ;
14
+ use Magento \Catalog \Model \ResourceModel \Product \Collection ;
14
15
use Magento \Store \Model \Store ;
15
16
16
17
/**
@@ -122,59 +123,47 @@ protected function _addSpecialAttributes(array &$attributes)
122
123
/**
123
124
* Add condition to collection
124
125
*
125
- * @param \Magento\Catalog\Model\ResourceModel\Product\ Collection $collection
126
+ * @param Collection $collection
126
127
* @return $this
127
128
*/
128
129
public function addToCollection ($ collection )
129
130
{
130
131
$ attribute = $ this ->getAttributeObject ();
132
+ $ attributeCode = $ attribute ->getAttributeCode ();
133
+ if ($ attributeCode !== 'price ' || !$ collection ->getLimitationFilters ()->isUsingPriceIndex ()) {
134
+ if ($ collection ->isEnabledFlat ()) {
135
+ if ($ attribute ->isEnabledInFlat ()) {
136
+ $ alias = array_keys ($ collection ->getSelect ()->getPart ('from ' ))[0 ];
137
+ $ this ->joinedAttributes [$ attributeCode ] = $ alias . '. ' . $ attributeCode ;
138
+ } else {
139
+ $ alias = 'at_ ' . $ attributeCode ;
140
+ if (!in_array ($ alias , array_keys ($ collection ->getSelect ()->getPart ('from ' )))) {
141
+ $ collection ->joinAttribute ($ attributeCode , "catalog_product/ $ attributeCode " , 'entity_id ' );
142
+ }
131
143
132
- if ($ collection ->isEnabledFlat ()) {
133
- if ($ attribute ->isEnabledInFlat ()) {
134
- $ alias = array_keys ($ collection ->getSelect ()->getPart ('from ' ))[0 ];
135
- $ this ->joinedAttributes [$ attribute ->getAttributeCode ()] = $ alias . '. ' . $ attribute ->getAttributeCode ();
136
- } else {
137
- $ alias = 'at_ ' . $ attribute ->getAttributeCode ();
138
- if (!in_array ($ alias , array_keys ($ collection ->getSelect ()->getPart ('from ' )))) {
139
- $ collection ->joinAttribute (
140
- $ attribute ->getAttributeCode (),
141
- 'catalog_product/ ' .$ attribute ->getAttributeCode (),
142
- 'entity_id '
143
- );
144
+ $ this ->joinedAttributes [$ attributeCode ] = $ alias . '.value ' ;
144
145
}
145
-
146
- $ this ->joinedAttributes [$ attribute ->getAttributeCode ()] = $ alias . '.value ' ;
146
+ } elseif ($ attributeCode !== 'category_ids ' && !$ attribute ->isStatic ()) {
147
+ $ this ->addAttributeToCollection ($ attribute , $ collection );
148
+ $ attributes = $ this ->getRule ()->getCollectedAttributes ();
149
+ $ attributes [$ attributeCode ] = true ;
150
+ $ this ->getRule ()->setCollectedAttributes ($ attributes );
147
151
}
148
- return $ this ;
149
152
}
150
153
151
- if ('category_ids ' == $ attribute ->getAttributeCode () || $ attribute ->isStatic ()) {
152
- return $ this ;
153
- }
154
-
155
- if ($ attribute ->getBackend () && $ attribute ->isScopeGlobal ()) {
156
- $ this ->addGlobalAttribute ($ attribute , $ collection );
157
- } else {
158
- $ this ->addNotGlobalAttribute ($ attribute , $ collection );
159
- }
160
-
161
- $ attributes = $ this ->getRule ()->getCollectedAttributes ();
162
- $ attributes [$ attribute ->getAttributeCode ()] = true ;
163
- $ this ->getRule ()->setCollectedAttributes ($ attributes );
164
-
165
154
return $ this ;
166
155
}
167
156
168
157
/**
169
158
* Adds Attributes that belong to Global Scope
170
159
*
171
160
* @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute
172
- * @param \Magento\Catalog\Model\ResourceModel\Product\ Collection $collection
161
+ * @param Collection $collection
173
162
* @return $this
174
163
*/
175
164
protected function addGlobalAttribute (
176
165
\Magento \Catalog \Model \ResourceModel \Eav \Attribute $ attribute ,
177
- \ Magento \ Catalog \ Model \ ResourceModel \ Product \ Collection $ collection
166
+ Collection $ collection
178
167
) {
179
168
switch ($ attribute ->getBackendType ()) {
180
169
case 'decimal ' :
@@ -207,12 +196,12 @@ protected function addGlobalAttribute(
207
196
* Adds Attributes that don't belong to Global Scope
208
197
*
209
198
* @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute
210
- * @param \Magento\Catalog\Model\ResourceModel\Product\ Collection $collection
199
+ * @param Collection $collection
211
200
* @return $this
212
201
*/
213
202
protected function addNotGlobalAttribute (
214
203
\Magento \Catalog \Model \ResourceModel \Eav \Attribute $ attribute ,
215
- \ Magento \ Catalog \ Model \ ResourceModel \ Product \ Collection $ collection
204
+ Collection $ collection
216
205
) {
217
206
$ storeId = $ this ->storeManager ->getStore ()->getId ();
218
207
$ values = $ collection ->getAllAttributeValues ($ attribute );
@@ -255,6 +244,8 @@ public function getMappedSqlField()
255
244
$ result = parent ::getMappedSqlField ();
256
245
} elseif (isset ($ this ->joinedAttributes [$ this ->getAttribute ()])) {
257
246
$ result = $ this ->joinedAttributes [$ this ->getAttribute ()];
247
+ } elseif ($ this ->getAttribute () === 'price ' ) {
248
+ $ result = 'price_index.min_price ' ;
258
249
} elseif ($ this ->getAttributeObject ()->isStatic ()) {
259
250
$ result = $ this ->getAttributeObject ()->getAttributeCode ();
260
251
} elseif ($ this ->getValueParsed ()) {
@@ -267,11 +258,27 @@ public function getMappedSqlField()
267
258
/**
268
259
* @inheritdoc
269
260
*
270
- * @param \Magento\Catalog\Model\ResourceModel\Product\ Collection $productCollection
261
+ * @param Collection $productCollection
271
262
* @return $this
272
263
*/
273
264
public function collectValidatedAttributes ($ productCollection )
274
265
{
275
266
return $ this ->addToCollection ($ productCollection );
276
267
}
268
+
269
+ /**
270
+ * Add attribute to collection based on scope
271
+ *
272
+ * @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute
273
+ * @param Collection $collection
274
+ * @return void
275
+ */
276
+ private function addAttributeToCollection ($ attribute , $ collection ): void
277
+ {
278
+ if ($ attribute ->getBackend () && $ attribute ->isScopeGlobal ()) {
279
+ $ this ->addGlobalAttribute ($ attribute , $ collection );
280
+ } else {
281
+ $ this ->addNotGlobalAttribute ($ attribute , $ collection );
282
+ }
283
+ }
277
284
}
0 commit comments