From f04fff98f2922156cf7d00b25a91863931ddc20e Mon Sep 17 00:00:00 2001 From: Romain Ruaud Date: Mon, 8 Apr 2024 16:11:19 +0200 Subject: [PATCH] Fix #3231 allow fetching products with category_uid IN clauses. --- .../Plugin/Search/RequestMapperPlugin.php | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/module-elasticsuite-virtual-category/Plugin/Search/RequestMapperPlugin.php b/src/module-elasticsuite-virtual-category/Plugin/Search/RequestMapperPlugin.php index 076b79691..030e75a81 100644 --- a/src/module-elasticsuite-virtual-category/Plugin/Search/RequestMapperPlugin.php +++ b/src/module-elasticsuite-virtual-category/Plugin/Search/RequestMapperPlugin.php @@ -99,15 +99,14 @@ public function afterGetFilters( ) { $storeId = $containerConfiguration->getStoreId(); if ($this->isEnabled($containerConfiguration)) { + $result = $this->decodeCategoryUid($result); if (isset($result['category.category_id']) && !isset($result['category.category_uid'])) { $result[] = $this->getCategoriesQuery($result['category.category_id'], $storeId); unset($result['category.category_id']); - } elseif (isset($result['category.category_uid']) && isset($result['category.category_uid']['eq']) && - !isset($result['category.category_id'])) { - $categoryUid[] = $this->uidEncoder->decode($result['category.category_uid']['eq']); + } elseif (isset($result['category.category_uid']) && !isset($result['category.category_id'])) { + $result[] = $this->getCategoriesQuery($result['category.category_uid'], $storeId); - $result[] = $this->getCategoriesQuery($categoryUid, $storeId); unset($result['category.category_uid']); } elseif (isset($result['category.category_id']) && isset($result['category.category_uid'])) { $result[] = $this->getCategoriesQuery($result['category.category_id'], $storeId); @@ -181,4 +180,33 @@ private function getCategorySubQuery($categoryId, $storeId) return $this->filterProvider->getQueryFilter($category); } + + /** + * Decode category_uid params if present in the request, because they are base64 encoded. + * + * @SuppressWarnings(PHPMD.UnusedLocalVariable) + * + * @param array $result The array containing potentially the params. + * + * @return array + * @throws \Magento\Framework\GraphQl\Exception\GraphQlInputException + */ + private function decodeCategoryUid(&$result) + { + if (isset($result['category.category_uid'])) { + $decodeCb = function (string $categoryUid) { + return $this->uidEncoder->decode($categoryUid); + }; + + foreach ($result['category.category_uid'] as $operator => &$categoryIds) { + if (!is_array($categoryIds)) { + $categoryIds = [$categoryIds]; + } + + $categoryIds = array_map($decodeCb, $categoryIds); + } + } + + return $result; + } }