diff --git a/src/module-elasticsuite-virtual-category/Model/Preview.php b/src/module-elasticsuite-virtual-category/Model/Preview.php index e1ca1898e..5cc074e8d 100644 --- a/src/module-elasticsuite-virtual-category/Model/Preview.php +++ b/src/module-elasticsuite-virtual-category/Model/Preview.php @@ -107,7 +107,7 @@ protected function getSortedProductIds() : array * * @return QueryInterface */ - private function getQueryFilter() : QueryInterface + private function getQueryFilter(): ?QueryInterface { $query = null; @@ -148,7 +148,7 @@ private function getQueryFilter() : QueryInterface * * @return QueryInterface */ - private function getEntityIdFilterQuery($ids) : QueryInterface + private function getEntityIdFilterQuery($ids): QueryInterface { return $this->queryFactory->create(QueryInterface::TYPE_TERMS, ['field' => 'entity_id', 'values' => $ids]); } diff --git a/src/module-elasticsuite-virtual-category/Model/Rule.php b/src/module-elasticsuite-virtual-category/Model/Rule.php index 8205d6215..013e31c71 100644 --- a/src/module-elasticsuite-virtual-category/Model/Rule.php +++ b/src/module-elasticsuite-virtual-category/Model/Rule.php @@ -177,7 +177,7 @@ public function __toString(): string public function getCategorySearchQuery($category, $excludedCategories = []): ?QueryInterface { \Magento\Framework\Profiler::start('ES:Virtual Rule ' . __FUNCTION__); - $categoryId = !is_object($category) ? $category : $category->getId(); + $categoryId = (int) !is_object($category) ? $category : $category->getId(); $cacheKey = implode( '|', [ @@ -188,7 +188,7 @@ public function getCategorySearchQuery($category, $excludedCategories = []): ?Qu ] ); - $query = self::$localCache[$categoryId] ?? false; + $query = $this->getFromLocalCache($categoryId); // If the category is not an object, it can't be in a "draft" mode. if ($query === false && (!is_object($category) || !$category->getHasDraftVirtualRule())) { @@ -204,13 +204,13 @@ public function getCategorySearchQuery($category, $excludedCategories = []): ?Qu } $query = $this->buildCategorySearchQuery($category, $excludedCategories); - if (!$category->getHasDraftVirtualRule()) { + if (!$category->getHasDraftVirtualRule() && $query !== null) { $cacheData = serialize($query); $this->sharedCache->save($cacheData, $cacheKey, $category->getCacheTags()); } } - self::$localCache[$categoryId] = $query; + $this->saveInLocalCache($categoryId, $query); \Magento\Framework\Profiler::stop('ES:Virtual Rule ' . __FUNCTION__); return $query; @@ -520,4 +520,27 @@ private function getTreeRootCategoryId($category): int return $rootCategoryId; } + + /** + * Get category query from local cache. + * + * @return QueryInterface|bool|null + */ + private function getFromLocalCache(int $categoryId): mixed + { + return self::$localCache[$categoryId] ?? false; + } + + /** + * Save category query in local cache. + * + * @param int $categoryId In of the category. + * @param QueryInterface|bool|null $query Query of the category. + */ + private function saveInLocalCache(int $categoryId, mixed $query): void + { + if ($query !== null) { + self::$localCache[$categoryId] = $query; + } + } }