@@ -21,34 +21,46 @@ public function allProducts(Request $request){
2121 $ query = Product::inRandomOrder ()
2222 ->where ('status ' , 1 )
2323 ->where ('is_trashed ' , 0 );
24- if ($ request ->has ('min_price ' ) || $ request ->has ('max_price ' )) {
25- $ query ->where (function (Builder $ subQuery ) use ($ request ) {
26-
27- // Logique pour les produits simples
28- $ subQuery ->where (function ($ simpleProductQuery ) use ($ request ) {
29- if ($ request ->has ('min_price ' )) {
30- $ minPrice = floatval ($ request ->input ('min_price ' ));
31- $ simpleProductQuery ->whereRaw ('CAST(product_price AS DECIMAL(10, 2)) >= ? ' , [$ minPrice ]);
32- }
33- if ($ request ->has ('max_price ' )) {
34- $ maxPrice = floatval ($ request ->input ('max_price ' ));
35- $ simpleProductQuery ->whereRaw ('CAST(product_price AS DECIMAL(10, 2)) <= ? ' , [$ maxPrice ]);
36- }
37- });
38-
39- // Logique pour les produits variés
40- $ subQuery ->orWhereHas ('variations ' , function (Builder $ variationQuery ) use ($ request ) {
41- if ($ request ->has ('min_price ' )) {
42- $ minPrice = floatval ($ request ->input ('min_price ' ));
43- $ variationQuery ->whereRaw ('CAST(price AS DECIMAL(10, 2)) >= ? ' , [$ minPrice ]);
44- }
45- if ($ request ->has ('max_price ' )) {
46- $ maxPrice = floatval ($ request ->input ('max_price ' ));
47- $ variationQuery ->whereRaw ('CAST(price AS DECIMAL(10, 2)) <= ? ' , [$ maxPrice ]);
48- }
49- });
24+
25+
26+ // Appliquer le filtre de prix si des paramètres sont présents
27+ if ($ request ->has ('min_price ' ) || $ request ->has ('max_price ' )) {
28+ $ minPrice = $ request ->input ('min_price ' ) ? floatval ($ request ->input ('min_price ' )) : null ;
29+ $ maxPrice = $ request ->input ('max_price ' ) ? floatval ($ request ->input ('max_price ' )) : null ;
30+
31+ $ query ->where (function (Builder $ subQuery ) use ($ minPrice , $ maxPrice ) {
32+
33+ // Filtre pour les produits simples (prix dans la table 'products')
34+ $ subQuery ->where (function ($ simpleProductQuery ) use ($ minPrice , $ maxPrice ) {
35+ if ($ minPrice !== null ) {
36+ $ simpleProductQuery ->whereRaw ('CAST(product_price AS DECIMAL(10, 2)) >= ? ' , [$ minPrice ]);
37+ }
38+ if ($ maxPrice !== null ) {
39+ $ simpleProductQuery ->whereRaw ('CAST(product_price AS DECIMAL(10, 2)) <= ? ' , [$ maxPrice ]);
40+ }
5041 });
51- }
42+
43+ // Filtre pour les produits variés (couleur uniquement, prix dans la table 'product_variations')
44+ $ subQuery ->orWhereHas ('variations ' , function (Builder $ variationQuery ) use ($ minPrice , $ maxPrice ) {
45+ if ($ minPrice !== null ) {
46+ $ variationQuery ->whereRaw ('CAST(price AS DECIMAL(10, 2)) >= ? ' , [$ minPrice ]);
47+ }
48+ if ($ maxPrice !== null ) {
49+ $ variationQuery ->whereRaw ('CAST(price AS DECIMAL(10, 2)) <= ? ' , [$ maxPrice ]);
50+ }
51+ });
52+
53+ // Filtre pour les produits variés (couleur + attributs, prix dans 'variation_attributes')
54+ $ subQuery ->orWhereHas ('variations.attributesVariation ' , function (Builder $ attributeQuery ) use ($ minPrice , $ maxPrice ) {
55+ if ($ minPrice !== null ) {
56+ $ attributeQuery ->whereRaw ('CAST(price AS DECIMAL(10, 2)) >= ? ' , [$ minPrice ]);
57+ }
58+ if ($ maxPrice !== null ) {
59+ $ attributeQuery ->whereRaw ('CAST(price AS DECIMAL(10, 2)) <= ? ' , [$ maxPrice ]);
60+ }
61+ });
62+ });
63+ }
5264
5365
5466 $ products = $ query ->paginate (6 );
0 commit comments