Skip to content

Commit

Permalink
Add category filter on product search
Browse files Browse the repository at this point in the history
  • Loading branch information
lopes-vincent authored Oct 30, 2023
1 parent 76fc464 commit 9204e6e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Controller/Front/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 9204e6e

Please sign in to comment.