@@ -152,9 +152,11 @@ protected function _getMappedSqlCondition(
152
152
): string {
153
153
$ argument = $ condition ->getMappedSqlField ();
154
154
155
- // If rule hasn't valid argument - create negative expression to prevent incorrect rule behavior.
155
+ // If rule hasn't valid argument - prevent incorrect rule behavior.
156
156
if (empty ($ argument )) {
157
157
return $ this ->_expressionFactory ->create (['expression ' => '1 = -1 ' ]);
158
+ } elseif (preg_match ('/[^a-z0-9\-_\.\`]/i ' , $ argument ) > 0 ) {
159
+ throw new \Magento \Framework \Exception \LocalizedException (__ ('Invalid field ' ));
158
160
}
159
161
160
162
$ conditionOperator = $ condition ->getOperatorForValidate ();
@@ -195,7 +197,6 @@ protected function _getMappedSqlCondition(
195
197
);
196
198
}
197
199
}
198
-
199
200
return $ this ->_expressionFactory ->create (
200
201
['expression ' => $ expression ]
201
202
);
@@ -241,6 +242,7 @@ protected function _getMappedSqlCombination(
241
242
* @param AbstractCollection $collection
242
243
* @param Combine $combine
243
244
* @return void
245
+ * @throws \Magento\Framework\Exception\LocalizedException
244
246
*/
245
247
public function attachConditionToCollection (
246
248
AbstractCollection $ collection ,
@@ -250,29 +252,45 @@ public function attachConditionToCollection(
250
252
$ this ->_joinTablesToCollection ($ collection , $ combine );
251
253
$ whereExpression = (string )$ this ->_getMappedSqlCombination ($ combine );
252
254
if (!empty ($ whereExpression )) {
253
- if (!empty ($ combine ->getConditions ())) {
254
- $ conditions = '' ;
255
- $ attributeField = '' ;
256
- foreach ($ combine ->getConditions () as $ condition ) {
257
- if ($ condition ->getData ('attribute ' ) === \Magento \Catalog \Api \Data \ProductInterface::SKU ) {
258
- $ conditions = $ condition ->getData ('value ' );
259
- $ attributeField = $ condition ->getMappedSqlField ();
260
- }
261
- }
255
+ $ collection ->getSelect ()->where ($ whereExpression );
256
+ $ this ->buildConditions ($ collection , $ combine );
257
+ }
258
+ }
262
259
263
- $ collection ->getSelect ()->where ($ whereExpression );
260
+ /**
261
+ * Build sql conditions from combination.
262
+ *
263
+ * @param AbstractCollection $collection
264
+ * @param Combine $combine
265
+ * @return void
266
+ */
267
+ private function buildConditions (AbstractCollection $ collection , Combine $ combine ) : void
268
+ {
269
+ if (!empty ($ combine ->getConditions ())) {
270
+ $ conditions = '' ;
271
+ $ attributeField = '' ;
272
+ foreach ($ combine ->getConditions () as $ condition ) {
273
+ if ($ condition ->getData ('attribute ' ) === \Magento \Catalog \Api \Data \ProductInterface::SKU ) {
274
+ $ conditions = $ condition ->getData ('value ' );
275
+ $ attributeField = $ condition ->getMappedSqlField ();
276
+ }
277
+ }
264
278
265
- if (!empty ($ conditions ) && !empty ($ attributeField )) {
266
- $ conditions = explode (', ' , $ conditions );
267
- foreach ($ conditions as &$ condition ) {
268
- $ condition = "' " . trim ($ condition ) . "' " ;
269
- }
270
- $ conditions = implode (', ' , $ conditions );
271
- $ collection ->getSelect ()->order ("FIELD( $ attributeField, $ conditions) " );
279
+ if (!empty ($ conditions ) && !empty ($ attributeField )) {
280
+ $ conditions = explode (', ' , $ conditions );
281
+ foreach ($ conditions as &$ condition ) {
282
+ $ condition = trim ($ condition );
272
283
}
273
- } else {
274
- // Select ::where method adds braces even on empty expression
275
- $ collection ->getSelect ()->where ($ whereExpression );
284
+ $ conditions = implode (', ' , $ conditions );
285
+ $ collection ->getSelect ()->order (
286
+ $ this ->_connection ->quoteInto (
287
+ "FIELD(?, ?) " ,
288
+ [
289
+ $ attributeField ,
290
+ $ conditions
291
+ ]
292
+ )
293
+ );
276
294
}
277
295
}
278
296
}
0 commit comments