1
|
|
|
<?php |
2
|
|
|
namespace Mongi\Mongicommerce\Http\Controllers\shop; |
3
|
|
|
|
4
|
|
|
use App\Models\Product; |
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Mongi\Mongicommerce\Http\Controllers\Controller; |
8
|
|
|
use Mongi\Mongicommerce\Models\Cart; |
9
|
|
|
use Mongi\Mongicommerce\Models\ProductItem; |
10
|
|
|
|
11
|
|
|
class ShopCartController extends Controller |
12
|
|
|
{ |
13
|
|
|
public function page(){ |
14
|
|
|
return view('mongicommerce.pages.cart'); |
15
|
|
|
} |
16
|
|
|
/** |
17
|
|
|
* @param ProductItem $product |
18
|
|
|
* @return bool|\Illuminate\Http\JsonResponse |
19
|
|
|
*/ |
20
|
|
|
private function checkIfProductAvaible(ProductItem $product) |
21
|
|
|
{ |
22
|
|
|
$num_avaibile_product = $product->quantity; |
23
|
|
|
|
24
|
|
|
$checkElement = Cart::where('product_item_id', $product->id)->first(); |
25
|
|
|
//if is null it means the cart is empty |
26
|
|
|
if (is_null($checkElement)) { |
27
|
|
|
if($num_avaibile_product > 0){ |
28
|
|
|
return true; |
29
|
|
|
}else{ |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
}else{ |
33
|
|
|
//if there is something in the cart check the avaibility |
34
|
|
|
if ($checkElement->quantity + 1 <= $num_avaibile_product) { |
35
|
|
|
return true; |
36
|
|
|
} else { |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function addToCart(Request $r) |
43
|
|
|
{ |
44
|
|
|
$product_item_id = $r->get('product_item_id'); |
45
|
|
|
$product = ProductItem::find($product_item_id); |
46
|
|
|
$check = $this->checkIfProductAvaible($product); |
47
|
|
|
|
48
|
|
|
if($check){ |
49
|
|
|
//if i'am not authenticated |
50
|
|
|
if (!Auth::check()) { |
51
|
|
|
session()->push('products.ids', $product_item_id); |
52
|
|
|
session()->put('cart',self::getCountableCart(session('products.ids'))); |
53
|
|
|
return response()->json(session('cart')); |
54
|
|
|
}else{ |
55
|
|
|
//if number i want is less than number avaible product i can add into cart |
56
|
|
|
$mycart = Cart::where('product_item_id',$product->id)->where('user_id',Auth::user()->id)->first(); |
|
|
|
|
57
|
|
|
if(is_null($mycart)){ |
58
|
|
|
$cart = new Cart(); |
59
|
|
|
$cart->user_id = Auth::user()->id; |
60
|
|
|
$cart->product_item_id = $product_item_id; |
61
|
|
|
$cart->quantity = 1; |
62
|
|
|
$cart->save(); |
63
|
|
|
}else{ |
64
|
|
|
$mycart->quantity = $mycart->quantity + 1; |
65
|
|
|
$mycart->save(); |
66
|
|
|
} |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
}else{ |
70
|
|
|
return response()->json([ |
71
|
|
|
'errors' => "Prodotto non disponibile o terminato", |
72
|
|
|
], 422); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function calculateTotalPrice(array $cart) |
78
|
|
|
{ |
79
|
|
|
$info_totale = []; |
|
|
|
|
80
|
|
|
$total = []; |
81
|
|
|
$totale_weight = []; |
82
|
|
|
foreach ($cart as $element) { |
83
|
|
|
$total[] = $element['total']; |
84
|
|
|
$totale_weight[] = $element['sum_weight']; |
85
|
|
|
} |
86
|
|
|
$info_totale = [ |
87
|
|
|
'total' => array_sum($total), |
88
|
|
|
'totale_weight' => array_sum($totale_weight) |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
return $info_totale; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public static function getCountableCart($arr) |
95
|
|
|
{ |
96
|
|
|
if (!is_null($arr)) { |
97
|
|
|
return array_count_values($arr); |
98
|
|
|
} else { |
99
|
|
|
return []; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getCartElements() |
104
|
|
|
{ |
105
|
|
|
if (!Auth::check()) { |
106
|
|
|
if (!empty(session('products.ids'))) { |
107
|
|
|
return response()->json(count(session('cart'))); |
|
|
|
|
108
|
|
|
} else { |
109
|
|
|
return 0; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} else { |
113
|
|
|
$products_in_cart = Cart::where('user_id', Auth::user()->id)->count(); |
|
|
|
|
114
|
|
|
return response()->json($products_in_cart); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getCartProducts() |
119
|
|
|
{ |
120
|
|
|
$products = []; |
121
|
|
|
if (!Auth::check()) { |
122
|
|
|
$ids = []; |
123
|
|
|
session()->forget('products.ids'); |
124
|
|
|
$productsCart = session('cart'); |
125
|
|
|
if(isset($productsCart)){ |
126
|
|
|
foreach ($productsCart as $id => $count) { |
127
|
|
|
$product = ProductItem::where('id', $id)->first(); |
128
|
|
|
$ids[] = $id; |
129
|
|
|
$products[] = [ |
130
|
|
|
'detail' => $product, |
131
|
|
|
'single_price' => $product->price, |
132
|
|
|
'count' => $count, |
133
|
|
|
'single_weight' => $product->weight, |
|
|
|
|
134
|
|
|
'sum_weight' => $product->weight * $count, |
135
|
|
|
'total' => $product->price * $count |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
session()->put('products.ids',$ids); |
140
|
|
|
|
141
|
|
|
} else { |
142
|
|
|
$productsCart = Cart::where('user_id', Auth::user()->id)->get(); |
|
|
|
|
143
|
|
|
foreach ($productsCart as $element) { |
144
|
|
|
$product = ProductItem::where('id', $element->product_item_id)->first(); |
145
|
|
|
$products[] = [ |
146
|
|
|
'detail' => $product, |
147
|
|
|
'single_price' => $product->price, |
148
|
|
|
'count' => $element->quantity, |
149
|
|
|
'single_weight' => 0, |
150
|
|
|
'sum_weight' => 0, |
151
|
|
|
'total' => $product->price * $element->quantity |
152
|
|
|
]; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return response()->json(['product' => $products , 'total' => self::calculateTotalPrice($products)]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function incrementOrDecrementElementInCart(Request $r){ |
160
|
|
|
$product_id = $r->get('product_id'); |
161
|
|
|
$operator = $r->get('operator'); |
162
|
|
|
$product = ProductItem::find($product_id); |
163
|
|
|
if (!Auth::check()) { |
164
|
|
|
$cart = session('cart'); |
165
|
|
|
$count_product_in_session = $cart[$product_id]; |
166
|
|
|
if($product->quantity > $count_product_in_session || $operator < 0){ |
167
|
|
|
if($count_product_in_session > 1 || $operator > 0){ |
168
|
|
|
$cart[$product_id] = $count_product_in_session + $operator; |
169
|
|
|
session()->put('cart',$cart); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
}else{ |
173
|
|
|
$cart = Cart::where('product_item_id',$product_id)->first(); |
174
|
|
|
if($product->quantity > $cart->quantity || $operator < 0){ |
175
|
|
|
if($cart->quantity > 1 || $operator > 0){ |
176
|
|
|
$cart->quantity = $cart->quantity + $operator; |
177
|
|
|
$cart->save(); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function deleteFromCart(Request $r){ |
185
|
|
|
$product_id = $r->get('product_id'); |
186
|
|
|
if (!Auth::check()) { |
187
|
|
|
$cart_in_session = session('cart'); |
188
|
|
|
unset($cart_in_session[$product_id]); |
189
|
|
|
session()->put('cart',$cart_in_session); |
190
|
|
|
}else{ |
191
|
|
|
Cart::where('product_item_id',$product_id)->delete(); |
192
|
|
|
return true; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths