Skip to content

Commit 8d62341

Browse files
committed
#11897: Catalog product list widget not working with multiple sku
1 parent 3c59bd5 commit 8d62341

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

app/code/Magento/Rule/Model/Condition/Product/AbstractProduct.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public function getDefaultOperatorInputByType()
137137
*/
138138
$this->_defaultOperatorInputByType['category'] = ['==', '!=', '{}', '!{}', '()', '!()'];
139139
$this->_arrayInputTypes[] = 'category';
140+
$this->_defaultOperatorInputByType['sku'] = ['==', '!=', '{}', '!{}', '()', '!()'];
140141
}
141142
return $this->_defaultOperatorInputByType;
142143
}
@@ -382,6 +383,9 @@ public function getInputType()
382383
if ($this->getAttributeObject()->getAttributeCode() == 'category_ids') {
383384
return 'category';
384385
}
386+
if ($this->getAttributeObject()->getAttributeCode() == 'sku') {
387+
return 'sku';
388+
}
385389
switch ($this->getAttributeObject()->getFrontendInput()) {
386390
case 'select':
387391
return 'select';
@@ -606,7 +610,10 @@ public function getBindArgumentValue()
606610
$this->getValueParsed()
607611
)->__toString()
608612
);
613+
} elseif ($this->getAttribute() === 'sku') {
614+
$this->isMultipleSku();
609615
}
616+
610617
return parent::getBindArgumentValue();
611618
}
612619

@@ -704,7 +711,7 @@ protected function _getAttributeSetId($productId)
704711
public function getOperatorForValidate()
705712
{
706713
$operator = $this->getOperator();
707-
if ($this->getInputType() == 'category') {
714+
if ($this->getInputType() == 'category' || $this->isMultipleSku()) {
708715
if ($operator == '==') {
709716
$operator = '{}';
710717
} elseif ($operator == '!=') {
@@ -753,4 +760,31 @@ private function removeTagsFromLabel(array $selectOptions)
753760

754761
return $selectOptions;
755762
}
763+
764+
/**
765+
* Check condition contains multiple sku.
766+
*
767+
* @return bool
768+
*/
769+
private function isMultipleSku()
770+
{
771+
$result = false;
772+
if ($this->getInputType() === 'sku') {
773+
if ($this->hasValueParsed()) {
774+
$value = $this->getData('value_parsed');
775+
if (count($value > 1)) {
776+
$result = true;
777+
}
778+
} else {
779+
$value = $this->getData('value');
780+
$value = preg_split('#\s*[,;]\s*#', $value, null, PREG_SPLIT_NO_EMPTY);
781+
if (count($value > 1)) {
782+
$this->setValueParsed($value);
783+
$result = true;
784+
}
785+
}
786+
}
787+
788+
return $result;
789+
}
756790
}

dev/tests/integration/testsuite/Magento/CatalogWidget/Block/Product/ProductListTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,36 @@ public function testCreateCollection()
6767
. '`attribute`:`multiselect_attribute`,`operator`:`^[^]`,'
6868
. '`value`:[`' . implode(',', $multiselectAttributeOptionIds) . '`]^]^]';
6969
$this->block->setData('conditions_encoded', $encodedConditions);
70+
$this->performAssertions(1);
71+
}
72+
73+
/**
74+
* Test product list widget can process condition with multiple product sku.
75+
*
76+
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
77+
*/
78+
public function testCreateCollectionWithMultipleSkuCondition()
79+
{
80+
$encodedConditions = '^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,' .
81+
'`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule|' .
82+
'|Condition||Product`,`attribute`:`sku`,`operator`:`==`,`value`:`simple1, simple2`^]^]';
83+
$this->block->setData('conditions_encoded', $encodedConditions);
84+
$this->performAssertions(2);
85+
}
7086

71-
// Load products collection filtered using specified conditions and perform assesrions
87+
/**
88+
* Check product collection includes correct amount of products.
89+
*
90+
* @param int $count
91+
* @return void
92+
*/
93+
private function performAssertions(int $count)
94+
{
95+
// Load products collection filtered using specified conditions and perform assertions.
7296
$productCollection = $this->block->createCollection();
7397
$productCollection->load();
7498
$this->assertEquals(
75-
1,
99+
$count,
76100
$productCollection->count(),
77101
"Product collection was not filtered according to the widget condition."
78102
);

0 commit comments

Comments
 (0)