Skip to content

Commit 2ea7a99

Browse files
MAGETWO-94565: Catalog product collection filters produce errors and cause inconsistent behaviour
- Fix CR comments.
1 parent 882bbae commit 2ea7a99

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Category.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,10 @@ public function getCategoryWithChildren(int $categoryId): array
11551155
)->where('entity_type_id = ?', CategorySetup::CATEGORY_ENTITY_TYPE_ID)
11561156
->where('attribute_code = ?', 'is_anchor')
11571157
->limit(1);
1158+
$isAnchorAttributeCode = $connection->fetchOne($selectAttributeCode);
1159+
if (empty($isAnchorAttributeCode) || (int)$isAnchorAttributeCode <= 0) {
1160+
return [];
1161+
}
11581162

11591163
$select = $connection->select()
11601164
->from(
@@ -1166,7 +1170,7 @@ public function getCategoryWithChildren(int $categoryId): array
11661170
['is_anchor' => 'cce_int.value']
11671171
)->where(
11681172
'cce_int.attribute_id = ?',
1169-
$connection->fetchOne($selectAttributeCode)
1173+
$isAnchorAttributeCode
11701174
)->where(
11711175
"cce.path LIKE '%/{$categoryId}' OR cce.path LIKE '%/{$categoryId}/%'"
11721176
)->order('path');

app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,10 @@ private function getChildrenCategories(int $categoryId): array
21072107
$anchorCategory = [];
21082108

21092109
$categories = $this->categoryResourceModel->getCategoryWithChildren($categoryId);
2110+
if (empty($categories)) {
2111+
return $categoryIds;
2112+
}
2113+
21102114
$firstCategory = array_shift($categories);
21112115
if ($firstCategory['is_anchor'] == 1) {
21122116
$anchorCategory[] = (int)$firstCategory['entity_id'];

dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Product/CollectionTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public function testAddPriceDataOnSchedule()
9999

100100
/**
101101
* @magentoDataFixture Magento/Catalog/_files/products.php
102+
* @magentoAppIsolation enabled
102103
* @magentoDbIsolation disabled
103104
*/
104105
public function testSetVisibility()
@@ -116,6 +117,7 @@ public function testSetVisibility()
116117

117118
/**
118119
* @magentoDataFixture Magento/Catalog/_files/category_product.php
120+
* @magentoAppIsolation enabled
119121
* @magentoDbIsolation disabled
120122
*/
121123
public function testSetCategoryWithStoreFilter()
@@ -131,8 +133,8 @@ public function testSetCategoryWithStoreFilter()
131133
$this->collection->load();
132134

133135
$collectionStoreFilterAfter = Bootstrap::getObjectManager()->create(
134-
\Magento\Catalog\Model\ResourceModel\Product\Collection::class
135-
);
136+
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class
137+
)->create();
136138
$collectionStoreFilterAfter->addStoreFilter(1)->addCategoryFilter($category);
137139
$collectionStoreFilterAfter->load();
138140
$this->assertEquals($this->collection->getItems(), $collectionStoreFilterAfter->getItems());
@@ -141,6 +143,7 @@ public function testSetCategoryWithStoreFilter()
141143

142144
/**
143145
* @magentoDataFixture Magento/Catalog/_files/categories.php
146+
* @magentoAppIsolation enabled
144147
* @magentoDbIsolation disabled
145148
*/
146149
public function testSetCategoryFilter()

0 commit comments

Comments
 (0)