Skip to content

Commit 84362b0

Browse files
committed
Enhance ProductByCategoryController to support advanced filtering options
This commit adds functionality to the index method of ProductByCategoryController, allowing products to be filtered by price range, colors, attributes, gender, and seller mode. It introduces complex query conditions that handle various filtering scenarios, improving the product listing experience for users by enabling more precise searches based on their preferences.
1 parent a4a1502 commit 84362b0

File tree

2 files changed

+125
-6
lines changed

2 files changed

+125
-6
lines changed

app/Http/Controllers/Product/ProductByCategoryController.php

Lines changed: 125 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,137 @@
22

33
namespace App\Http\Controllers\Product;
44

5-
use App\Models\Product;
6-
use Illuminate\Http\Request;
75
use App\Http\Controllers\Controller;
86
use App\Http\Resources\ProductResource;
7+
use App\Models\Product;
8+
use Illuminate\Http\Request;
9+
use Illuminate\Database\Eloquent\Builder;
10+
911

1012
class ProductByCategoryController extends Controller
1113
{
1214
public function index($categoryUrl){
13-
$products=Product::with('categories')->whereHas('categories',function($query) use ($categoryUrl){
15+
$query=Product::with('categories')->whereHas('categories',function($query) use ($categoryUrl){
1416
$query->where('categories.category_url',$categoryUrl);
15-
})->get();
16-
return response()->json(ProductResource::collection($products));
17+
});
18+
19+
20+
if ($request->has('min_price') || $request->has('max_price')) {
21+
$minPrice = $request->input('min_price') ? floatval($request->input('min_price')) : null;
22+
$maxPrice = $request->input('max_price') ? floatval($request->input('max_price')) : null;
23+
24+
$query->where(function (Builder $subQuery) use ($minPrice, $maxPrice) {
25+
26+
// Filtre pour les produits simples (prix dans la table 'products')
27+
$subQuery->where(function ($simpleProductQuery) use ($minPrice, $maxPrice) {
28+
if ($minPrice !== null) {
29+
$simpleProductQuery->whereRaw('CAST(product_price AS DECIMAL(10, 2)) >= ?', [$minPrice]);
30+
}
31+
if ($maxPrice !== null) {
32+
$simpleProductQuery->whereRaw('CAST(product_price AS DECIMAL(10, 2)) <= ?', [$maxPrice]);
33+
}
34+
});
35+
36+
// Filtre pour les produits variés (couleur uniquement, prix dans la table 'product_variations')
37+
$subQuery->orWhereHas('variations', function (Builder $variationQuery) use ($minPrice, $maxPrice) {
38+
if ($minPrice !== null) {
39+
$variationQuery->whereRaw('CAST(price AS DECIMAL(10, 2)) >= ?', [$minPrice]);
40+
}
41+
if ($maxPrice !== null) {
42+
$variationQuery->whereRaw('CAST(price AS DECIMAL(10, 2)) <= ?', [$maxPrice]);
43+
}
44+
});
45+
46+
// Filtre pour les produits variés (couleur + attributs, prix dans 'variation_attributes')
47+
$subQuery->orWhereHas('variations.attributesVariation', function (Builder $attributeQuery) use ($minPrice, $maxPrice) {
48+
if ($minPrice !== null) {
49+
$attributeQuery->whereRaw('CAST(price AS DECIMAL(10, 2)) >= ?', [$minPrice]);
50+
}
51+
if ($maxPrice !== null) {
52+
$attributeQuery->whereRaw('CAST(price AS DECIMAL(10, 2)) <= ?', [$maxPrice]);
53+
}
54+
});
55+
});
56+
}
57+
58+
59+
60+
if ($request->has('colors')) {
61+
$colorsString = $request->input('colors');
62+
$colorNames = explode(',', $colorsString);
63+
64+
$query->whereHas('variations', function (Builder $variationQuery) use ($colorNames) {
65+
$variationQuery->whereHas('color', function (Builder $colorQuery) use ($colorNames) {
66+
$colorQuery->whereIn('value', $colorNames);
67+
});
68+
});
69+
}
70+
71+
if ($request->has('attributes')) {
72+
$attributesToFilter = $request->input('attributes');
73+
74+
// Check if the input is a string and handle it as a single attribute
75+
if (!is_array($attributesToFilter) && is_string($attributesToFilter)) {
76+
// Assuming a single attribute ID is passed, e.g., ?attributes=5
77+
$attributesToFilter = [$attributesToFilter => []];
78+
}
79+
80+
foreach ($attributesToFilter as $attributeId => $valueIdsString) {
81+
$valueIds = explode(',', $valueIdsString);
82+
83+
$query->whereHas('variations.attributesVariation', function (Builder $attributeQuery) use ($attributeId, $valueIds) {
84+
$attributeQuery->whereHas('attributeValue', function (Builder $attributeValueQuery) use ($attributeId, $valueIds) {
85+
$attributeValueQuery
86+
->whereIn('id', $valueIds);
87+
});
88+
});
89+
}
90+
}
91+
92+
if ($request->has('gender')) {
93+
$genderId = $request->input('gender');
94+
$query->where('product_gender', $genderId);
95+
}
96+
97+
if ($request->has('seller_mode') && $request->input('seller_mode')==true) {
98+
$query->where('is_wholesale',1);
99+
100+
if ($request->has('bulk_price_range')) {
101+
list($minBulkPrice, $maxBulkPrice) = explode('-', $request->input('bulk_price_range'));
102+
103+
$minBulkPrice = floatval($minBulkPrice);
104+
$maxBulkPrice = floatval($maxBulkPrice);
105+
// On regroupe toutes les conditions de prix de gros
106+
$query->where(function(Builder $bulkPriceQuery) use ($minBulkPrice, $maxBulkPrice) {
107+
108+
// Cas 1: Prix de gros pour les produits simples (sans variations)
109+
$bulkPriceQuery->whereHas('wholesalePrices', function (Builder $wholesaleQuery) use ($minBulkPrice, $maxBulkPrice) {
110+
$wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) >= ?', [$minBulkPrice]);
111+
$wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) <= ?', [$maxBulkPrice]);
112+
});
113+
114+
// Cas 2: Prix de gros pour les produits variés (couleur uniquement)
115+
$bulkPriceQuery->orWhereHas('variations', function (Builder $variationQuery) use ($minBulkPrice, $maxBulkPrice) {
116+
$variationQuery->whereHas('wholesalePrices', function (Builder $wholesaleQuery) use ($minBulkPrice, $maxBulkPrice) {
117+
$wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) >= ?', [$minBulkPrice]);
118+
$wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) <= ?', [$maxBulkPrice]);
119+
});
120+
});
121+
122+
// Cas 3: Prix de gros pour les produits variés (couleur + attribut)
123+
$bulkPriceQuery->orWhereHas('variations.attributesVariation', function (Builder $attributeQuery) use ($minBulkPrice, $maxBulkPrice) {
124+
$attributeQuery->whereHas('wholesalePrices', function (Builder $wholesaleQuery) use ($minBulkPrice, $maxBulkPrice) {
125+
$wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) >= ?', [$minBulkPrice]);
126+
$wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) <= ?', [$maxBulkPrice]);
127+
});
128+
});
129+
});
130+
}
131+
132+
}
133+
134+
$products = $query->paginate(6);
135+
136+
return ProductResource::collection($products);
17137
}
18138
}

app/Http/Controllers/Product/ProductListController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public function allProducts(Request $request){
117117

118118
$minBulkPrice = floatval($minBulkPrice);
119119
$maxBulkPrice = floatval($maxBulkPrice);
120-
Log::info('Bulk price range', ['minBulkPrice' => gettype($minBulkPrice), 'maxBulkPrice' => $maxBulkPrice]);
121120
// On regroupe toutes les conditions de prix de gros
122121
$query->where(function(Builder $bulkPriceQuery) use ($minBulkPrice, $maxBulkPrice) {
123122

0 commit comments

Comments
 (0)