From 9204e6e76bcf486df386bab1e10bd8a65e7a1f7c Mon Sep 17 00:00:00 2001 From: Vincent Lopes-Vicente Date: Mon, 30 Oct 2023 15:59:21 +0100 Subject: [PATCH] Add category filter on product search --- Controller/Front/ProductController.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Controller/Front/ProductController.php b/Controller/Front/ProductController.php index 16cb416..4ddba03 100644 --- a/Controller/Front/ProductController.php +++ b/Controller/Front/ProductController.php @@ -8,6 +8,7 @@ use Propel\Runtime\ActiveQuery\Criteria; use Symfony\Component\Routing\Annotation\Route; use Thelia\Core\HttpFoundation\Request; +use Thelia\Model\CategoryQuery; use Thelia\Model\ProductQuery; /** @@ -114,6 +115,21 @@ class ProductController extends BaseFrontOpenApiController * default="alpha" * ) * ), + * @OA\Parameter( + * name="category", + * in="query", + * @OA\Schema( + * type="string" + * ) + * ), + * @OA\Parameter( + * name="category_depth", + * in="query", + * @OA\Schema( + * type="integer", + * default="1" + * ) + * ), * @OA\Response( * response="200", * description="Success", @@ -204,6 +220,15 @@ public function search( $productI18nQuery->endUse(); } + if (null !== $request->get('category')) { + $depth = $request->get('category_depth', 1); + $allCategoryIDs = CategoryQuery::getCategoryTreeIds($request->get('category'), $depth); + $productQuery->useProductCategoryQuery('CategorySelect') + ->filterByCategoryId($allCategoryIDs, Criteria::IN) + ->endUse() + ; + } + $products = $productQuery->find(); $products = array_map(fn ($product) => $modelFactory->buildModel('Product', $product), iterator_to_array($products));