6 declare(strict_types=1);
26 private $resourceConnection;
31 private $categoryRepository;
37 private $rootCategoryLevel = 1;
43 \
Magento\Framework\
App\ResourceConnection $resourceConnection,
58 $categorySelect = $this->resourceConnection->getConnection()->select()
60 [
'cat' => $this->resourceConnection->getTableName(
'catalog_category_product')],
63 $this->resourceConnection->getConnection()->prepareSqlCondition(
65 [$this->mapConditionType($filter->
getConditionType()) => $this->getCategoryIds($filter)]
70 'in' => $categorySelect
73 return $this->resourceConnection->getConnection()
85 private function getCategoryIds(
Filter $filter): array
87 $categoryIds = explode(
',', $filter->
getValue());
88 $childCategoryIds = [];
90 foreach ($categoryIds as $categoryId) {
92 $category = $this->categoryRepository->get($categoryId);
93 }
catch (CategoryDoesNotExistException $exception) {
98 $childCategoryIds[] =
$category->getAllChildren(
true);
102 if ((
int)
$category->getLevel() === $this->rootCategoryLevel) {
103 $childCategoryIds[] =
$category->getAllChildren(
true);
107 return array_unique(array_merge($categoryIds, ...$childCategoryIds));
116 private function mapConditionType(
string $conditionType): string
124 return $conditionsMap[$conditionType] ?? $conditionType;
__construct(\Magento\Framework\App\ResourceConnection $resourceConnection, \Magento\Catalog\Model\CategoryRepository $categoryRepository)