Optimize product retrieval by department categories and improve filtering in productsRoute

- Refactored `getAllProductInDepartmentCategories` in `departments_o` using `array_reduce` for cleaner and more efficient category loop.
- Enhanced `productsRoute` to filter department-specific products using IDs for stricter matching.
This commit is contained in:
Jeppe Bundgaard
2025-11-17 14:17:05 +01:00
parent 2d121c67cd
commit dfeffed63f
2 changed files with 17 additions and 14 deletions
+9 -1
View File
@@ -245,12 +245,20 @@ class productsRoute
if (isset($data['department_id'])) {
// Get all product ids contained in a category attached to the department
$departmentSpecificProducts = (new departments_o())->select((int)$data['department_id'])->getAllProductInDepartmentCategories();
// Get the product ids as an array
$departmentSpecificProductIds = array_map(function ($product) {
return $product->id;
}, $departmentSpecificProducts);
// Return the list of products
$response->success(
(new products_o())->applyDepartmentPricing((array)(new products_o())->listObjectsWithPaginationIfSet(
function ($product) {
function ($product) use ($departmentSpecificProductIds) {
// Only include products that are in the department specific product ids
return parseProduct($product);
},
(new products_o())->forceRestrictFilters([
'id' => $departmentSpecificProductIds
])
), (int)$data['department_id'])
);
}