Skip to content

Commit 765eb4b

Browse files
author
Roman Lytvynenko
committed
MC-23252: Pagebuilder products list doesn't work with anchor category
1 parent 94b3b0f commit 765eb4b

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

app/code/Magento/PageBuilder/Model/Catalog/ProductTotals.php

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
namespace Magento\PageBuilder\Model\Catalog;
1010

11+
use Magento\Catalog\Api\CategoryRepositoryInterface;
1112
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1213
use Magento\Catalog\Model\Product\Visibility;
1314
use Magento\Catalog\Model\ResourceModel\Product\Collection;
1415
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
1516
use Magento\CatalogInventory\Helper\Stock;
1617
use Magento\CatalogWidget\Model\Rule;
1718
use Magento\Framework\Exception\LocalizedException;
19+
use Magento\Framework\Exception\NoSuchEntityException;
1820
use Magento\Rule\Model\Condition\Combine;
1921
use Magento\Rule\Model\Condition\Sql\Builder;
2022
use Magento\Widget\Helper\Conditions;
@@ -50,25 +52,33 @@ class ProductTotals
5052
*/
5153
private $stockFilter;
5254

55+
/**
56+
* @var CategoryRepositoryInterface
57+
*/
58+
private $categoryRepository;
59+
5360
/**
5461
* @param CollectionFactory $productCollectionFactory
5562
* @param Builder $sqlBuilder
5663
* @param Rule $rule
5764
* @param Conditions $conditionsHelper
5865
* @param Stock $stockFilter
66+
* @param CategoryRepositoryInterface $categoryRepository
5967
*/
6068
public function __construct(
6169
CollectionFactory $productCollectionFactory,
6270
Builder $sqlBuilder,
6371
Rule $rule,
6472
Conditions $conditionsHelper,
65-
Stock $stockFilter
73+
Stock $stockFilter,
74+
CategoryRepositoryInterface $categoryRepository
6675
) {
6776
$this->productCollectionFactory = $productCollectionFactory;
6877
$this->sqlBuilder = $sqlBuilder;
6978
$this->rule = $rule;
7079
$this->conditionsHelper = $conditionsHelper;
7180
$this->stockFilter = $stockFilter;
81+
$this->categoryRepository = $categoryRepository;
7282
}
7383

7484
/**
@@ -84,17 +94,49 @@ private function decodeConditions(string $conditions): Combine
8494
}
8595

8696
foreach ($conditions as $key => $condition) {
87-
if (!empty($condition['attribute'])
88-
&& in_array($condition['attribute'], ['special_from_date', 'special_to_date'])
89-
) {
90-
$conditions[$key]['value'] = date('Y-m-d H:i:s', strtotime($condition['value']));
97+
if (!empty($condition['attribute'])) {
98+
if (in_array($condition['attribute'], ['special_from_date', 'special_to_date'])) {
99+
$conditions[$key]['value'] = date('Y-m-d H:i:s', strtotime($condition['value']));
100+
}
101+
102+
if ($condition['attribute'] == 'category_ids') {
103+
$conditions[$key] = $this->updateAnchorCategoryConditions($condition);
104+
}
91105
}
92106
}
93107

94108
$this->rule->loadPost(['conditions' => $conditions]);
95109
return $this->rule->getConditions();
96110
}
97111

112+
/**
113+
* Update conditions if the category is an anchor category
114+
*
115+
* @param array $condition
116+
* @return array
117+
*/
118+
private function updateAnchorCategoryConditions(array $condition): array
119+
{
120+
if (array_key_exists('value', $condition)) {
121+
$categoryId = $condition['value'];
122+
123+
try {
124+
$category = $this->categoryRepository->get($categoryId);
125+
} catch (NoSuchEntityException $e) {
126+
return $condition;
127+
}
128+
129+
if ($category->getIsAnchor() && $category->getChildren()) {
130+
$children = explode(',', $category->getChildren());
131+
132+
$condition['operator'] = "()";
133+
$condition['value'] = array_merge([$categoryId], $children);
134+
}
135+
}
136+
137+
return $condition;
138+
}
139+
98140
/**
99141
* Retrieve product collection based on provided conditions
100142
*

0 commit comments

Comments
 (0)