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:
@@ -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'])
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user