Skip to content

Commit 6041c3e

Browse files
Merge pull request #345 from magento/magento-tango-MC-23252
[Support] MC-23252: Pagebuilder products list doesn't work with anchor category
2 parents f41f238 + 1270d25 commit 6041c3e

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

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

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@
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\CatalogWidget\Model\Rule;
1617
use Magento\Framework\Exception\LocalizedException;
18+
use Magento\Framework\Exception\NoSuchEntityException;
1719
use Magento\Rule\Model\Condition\Combine;
1820
use Magento\Rule\Model\Condition\Sql\Builder;
1921
use Magento\Widget\Helper\Conditions;
2022
use Zend_Db_Select_Exception;
2123

2224
/**
2325
* Product totals for Products content type
26+
*
27+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2428
*/
2529
class ProductTotals
2630
{
@@ -44,22 +48,30 @@ class ProductTotals
4448
*/
4549
private $conditionsHelper;
4650

51+
/**
52+
* @var CategoryRepositoryInterface
53+
*/
54+
private $categoryRepository;
55+
4756
/**
4857
* @param CollectionFactory $productCollectionFactory
4958
* @param Builder $sqlBuilder
5059
* @param Rule $rule
5160
* @param Conditions $conditionsHelper
61+
* @param CategoryRepositoryInterface $categoryRepository
5262
*/
5363
public function __construct(
5464
CollectionFactory $productCollectionFactory,
5565
Builder $sqlBuilder,
5666
Rule $rule,
57-
Conditions $conditionsHelper
67+
Conditions $conditionsHelper,
68+
CategoryRepositoryInterface $categoryRepository
5869
) {
5970
$this->productCollectionFactory = $productCollectionFactory;
6071
$this->sqlBuilder = $sqlBuilder;
6172
$this->rule = $rule;
6273
$this->conditionsHelper = $conditionsHelper;
74+
$this->categoryRepository = $categoryRepository;
6375
}
6476

6577
/**
@@ -75,17 +87,49 @@ private function decodeConditions(string $conditions): Combine
7587
}
7688

7789
foreach ($conditions as $key => $condition) {
78-
if (!empty($condition['attribute'])
79-
&& in_array($condition['attribute'], ['special_from_date', 'special_to_date'])
80-
) {
81-
$conditions[$key]['value'] = date('Y-m-d H:i:s', strtotime($condition['value']));
90+
if (!empty($condition['attribute'])) {
91+
if (in_array($condition['attribute'], ['special_from_date', 'special_to_date'])) {
92+
$conditions[$key]['value'] = date('Y-m-d H:i:s', strtotime($condition['value']));
93+
}
94+
95+
if ($condition['attribute'] == 'category_ids') {
96+
$conditions[$key] = $this->updateAnchorCategoryConditions($condition);
97+
}
8298
}
8399
}
84100

85101
$this->rule->loadPost(['conditions' => $conditions]);
86102
return $this->rule->getConditions();
87103
}
88104

105+
/**
106+
* Update conditions if the category is an anchor category
107+
*
108+
* @param array $condition
109+
* @return array
110+
*/
111+
private function updateAnchorCategoryConditions(array $condition): array
112+
{
113+
if (array_key_exists('value', $condition)) {
114+
$categoryId = $condition['value'];
115+
116+
try {
117+
$category = $this->categoryRepository->get($categoryId);
118+
} catch (NoSuchEntityException $e) {
119+
return $condition;
120+
}
121+
122+
if ($category->getIsAnchor() && $category->getChildren(true)) {
123+
$children = explode(',', $category->getChildren(true));
124+
125+
$condition['operator'] = "()";
126+
$condition['value'] = array_merge([$categoryId], $children);
127+
}
128+
}
129+
130+
return $condition;
131+
}
132+
89133
/**
90134
* Retrieve product collection based on provided conditions
91135
*

0 commit comments

Comments
 (0)