Skip to content

Commit

Permalink
Merge pull request #3241 from romainruaud/fix_graphql-category-uid-in
Browse files Browse the repository at this point in the history
Fix #3231 allow fetching products with category_uid IN clauses.
  • Loading branch information
rbayet authored Apr 10, 2024
2 parents fb66665 + f04fff9 commit 0b60a10
Showing 1 changed file with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 0b60a10

Please sign in to comment.