Skip to content

Commit 8e61d95

Browse files
committed
PB-48: The order of product SKU is not respected if combined with category condition
1 parent 510d579 commit 8e61d95

File tree

2 files changed

+18
-15
lines changed
  • app/code/Magento/Rule/Model/Condition/Sql
  • dev/tests/integration/testsuite/Magento/Rule/Model/Condition/Sql

2 files changed

+18
-15
lines changed

app/code/Magento/Rule/Model/Condition/Sql/Builder.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -270,26 +270,22 @@ private function buildConditions(AbstractCollection $collection, Combine $combin
270270
$conditions = '';
271271
$attributeField = '';
272272
foreach ($combine->getConditions() as $condition) {
273-
if ($condition->getData('attribute') === \Magento\Catalog\Api\Data\ProductInterface::SKU) {
273+
if ($condition->getData('attribute') === \Magento\Catalog\Api\Data\ProductInterface::SKU
274+
&& $condition->getData('operator') === '()'
275+
) {
274276
$conditions = $condition->getData('value');
275-
$attributeField = $condition->getMappedSqlField();
277+
$attributeField = $this->_connection->quoteIdentifier($condition->getMappedSqlField());
276278
}
277279
}
278280

279281
if (!empty($conditions) && !empty($attributeField)) {
280-
$conditions = explode(',', $conditions);
281-
foreach ($conditions as &$condition) {
282-
$condition = trim($condition);
283-
}
284-
$conditions = implode(', ', $conditions);
282+
$conditions = $this->_connection->quote(
283+
array_map('trim', explode(',', $conditions))
284+
);
285285
$collection->getSelect()->reset(Select::ORDER);
286286
$collection->getSelect()->order(
287-
$this->_connection->quoteInto(
288-
"FIELD(?, ?)",
289-
[
290-
$attributeField,
291-
$conditions
292-
]
287+
$this->_expressionFactory->create(
288+
['expression' => "FIELD($attributeField, $conditions)"]
293289
)
294290
);
295291
}

dev/tests/integration/testsuite/Magento/Rule/Model/Condition/Sql/BuilderTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,20 @@ public function testAttachConditionToCollection(): void
6060
'operator' => '==',
6161
'value' => '2017-09-15',
6262
],
63+
'1--3' => [
64+
'type' => ProductCondition::class,
65+
'attribute' => 'sku',
66+
'operator' => '()',
67+
'value' => ' :( , :) ',
68+
]
6369
],
6470
];
6571

6672
$rule->loadPost($ruleConditionArray);
6773
$this->model->attachConditionToCollection($collection, $rule->getConditions());
6874

69-
$whereString = "/\(category_id IN \('3'\).+\(IFNULL\(`e`\.`entity_id`,.+\) = '2017-09-15'\)/";
70-
$this->assertNotFalse(preg_match($whereString, $collection->getSelectSql(true)));
75+
$whereString = "/\(category_id IN \('3'\).+\(IFNULL\(`e`\.`entity_id`,.+\) = '2017-09-15'\)"
76+
. ".+ORDER BY \(FIELD\(`e`.`sku`, ':\(', ':\)'\)\)/";
77+
$this->assertEquals(1, preg_match($whereString, $collection->getSelectSql(true)));
7178
}
7279
}

0 commit comments

Comments
 (0)