|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mongi\Mongicommerce\Http\Controllers\shop; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Http\Request; |
|
5
|
|
|
use Illuminate\Support\Facades\Auth; |
|
6
|
|
|
use Mongi\Mongicommerce\Http\Controllers\Controller; |
|
7
|
|
|
use Mongi\Mongicommerce\Models\Cart; |
|
8
|
|
|
use Mongi\Mongicommerce\Models\ProductItem; |
|
9
|
|
|
|
|
10
|
|
|
class ShopCartController extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
public function page(){ |
|
13
|
|
|
return view('mongicommerce.pages.cart'); |
|
14
|
|
|
} |
|
15
|
|
|
/** |
|
16
|
|
|
* @param ProductItem $product |
|
17
|
|
|
* @return bool|\Illuminate\Http\JsonResponse |
|
18
|
|
|
*/ |
|
19
|
|
|
private function checkIfProductAvaible(ProductItem $product) |
|
20
|
|
|
{ |
|
21
|
|
|
$num_avaibile_product = $product->quantity; |
|
22
|
|
|
|
|
23
|
|
|
$checkElement = Cart::where('product_item_id', $product->id)->first(); |
|
24
|
|
|
//if is null it means the cart is empty |
|
25
|
|
|
if (is_null($checkElement)) { |
|
26
|
|
|
if($num_avaibile_product > 0){ |
|
27
|
|
|
return true; |
|
28
|
|
|
}else{ |
|
29
|
|
|
return false; |
|
30
|
|
|
} |
|
31
|
|
|
}else{ |
|
32
|
|
|
//if there is something in the cart check the avaibility |
|
33
|
|
|
if ($checkElement->quantity + 1 <= $num_avaibile_product) { |
|
34
|
|
|
return true; |
|
35
|
|
|
} else { |
|
36
|
|
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function addToCart(Request $r) |
|
42
|
|
|
{ |
|
43
|
|
|
$product_item_id = $r->get('product_item_id'); |
|
44
|
|
|
$product = ProductItem::find($product_item_id); |
|
45
|
|
|
$check = $this->checkIfProductAvaible($product); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
if($check){ |
|
48
|
|
|
//if i'am not authenticated |
|
49
|
|
|
if (!Auth::check()) { |
|
50
|
|
|
session()->push('products.ids', $product_item_id); |
|
51
|
|
|
session()->put('cart',self::getCountableCart(session('products.ids'))); |
|
52
|
|
|
return response()->json(session('cart')); |
|
53
|
|
|
}else{ |
|
54
|
|
|
//if number i want is less than number avaible product i can add into cart |
|
55
|
|
|
$mycart = Cart::where('product_item_id',$product->id)->where('user_id',Auth::user()->id)->first(); |
|
|
|
|
|
|
56
|
|
|
if(is_null($mycart)){ |
|
57
|
|
|
$cart = new Cart(); |
|
58
|
|
|
$cart->user_id = Auth::user()->id; |
|
59
|
|
|
$cart->product_item_id = $product_item_id; |
|
60
|
|
|
$cart->quantity = 1; |
|
61
|
|
|
$cart->save(); |
|
62
|
|
|
}else{ |
|
63
|
|
|
$mycart->quantity = $mycart->quantity + 1; |
|
64
|
|
|
$mycart->save(); |
|
65
|
|
|
} |
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
}else{ |
|
69
|
|
|
return response()->json([ |
|
70
|
|
|
'errors' => "Prodotto non disponibile o terminato", |
|
71
|
|
|
], 422); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public static function getCountableCart($arr) |
|
77
|
|
|
{ |
|
78
|
|
|
if (!is_null($arr)) { |
|
79
|
|
|
return array_count_values($arr); |
|
80
|
|
|
} else { |
|
81
|
|
|
return []; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getCartElements() |
|
86
|
|
|
{ |
|
87
|
|
|
if (!Auth::check()) { |
|
88
|
|
|
if (!empty(session('products.ids'))) { |
|
89
|
|
|
return response()->json(count(session('cart'))); |
|
|
|
|
|
|
90
|
|
|
} else { |
|
91
|
|
|
return 0; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} else { |
|
95
|
|
|
$products_in_cart = Cart::where('user_id', Auth::user()->id)->count(); |
|
|
|
|
|
|
96
|
|
|
return response()->json($products_in_cart); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|