|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lenius\LaravelEcommerce\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\View\View; |
|
6
|
|
|
use Illuminate\Http\RedirectResponse; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Lenius\Basket\Item; |
|
9
|
|
|
use Lenius\Basket\ItemInterface; |
|
10
|
|
|
use Lenius\LaravelEcommerce\Facades\Basket; |
|
11
|
|
|
|
|
12
|
|
|
class EcommerceController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
public static function routes(): void |
|
15
|
|
|
{ |
|
16
|
|
|
$router = app()->make('router'); |
|
17
|
|
|
|
|
18
|
|
|
$router->get('basket', [EcommerceController::class, 'index'])->name('ecommerce.basket'); |
|
19
|
|
|
$router->get('basket/debug', [EcommerceController::class, 'debug'])->name('ecommerce.basket.debug'); |
|
20
|
|
|
$router->get('basket/demo', [EcommerceController::class, 'demo'])->name('ecommerce.basket.demo'); |
|
21
|
|
|
$router->get('basket/destroy', [EcommerceController::class, 'destroy'])->name('ecommerce.basket.destroy'); |
|
22
|
|
|
$router->post('basket/update', [EcommerceController::class, 'update'])->name('ecommerce.basket.update'); |
|
23
|
|
|
|
|
24
|
|
|
$router->any('basket/{id}/add', [EcommerceController::class, 'add'])->name('ecommerce.basket.add'); |
|
25
|
|
|
$router->get('basket/{id}/dec', [EcommerceController::class, 'dec'])->name('ecommerce.basket.item.dec'); |
|
26
|
|
|
$router->get('basket/{id}/inc', [EcommerceController::class, 'inc'])->name('ecommerce.basket.item.inc'); |
|
27
|
|
|
$router->get('basket/{id}/remove', [EcommerceController::class, 'remove'])->name('ecommerce.basket.item.remove'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function index(): View |
|
31
|
|
|
{ |
|
32
|
|
|
return view('ecommerce::basket'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function debug(): array |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
|
|
'items' => Basket::contents(), |
|
39
|
|
|
'sum' => Basket::total(false), |
|
40
|
|
|
'tax' => Basket::tax(), |
|
41
|
|
|
'total' => Basket::total(), |
|
42
|
|
|
'weight' => Basket::weight(), |
|
43
|
|
|
'total_items' => Basket::totalItems(), |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function destroy(): RedirectResponse |
|
48
|
|
|
{ |
|
49
|
|
|
Basket::destroy(); |
|
50
|
|
|
|
|
51
|
|
|
return redirect()->route('ecommerce.basket'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function update(Request $request): RedirectResponse |
|
55
|
|
|
{ |
|
56
|
|
|
$basket = $request->input('basket'); |
|
57
|
|
|
|
|
58
|
|
|
if ($basket['items']) { |
|
59
|
|
|
/* @var ItemInterface $item */ |
|
60
|
|
|
foreach ($basket['items'] as $itemIdentifier => $post_item) { |
|
61
|
|
|
if ($item = Basket::item($itemIdentifier)) { |
|
62
|
|
|
if ($post_item['quantity'] > 0) { |
|
63
|
|
|
$item->quantity = (int) $post_item['quantity']; |
|
64
|
|
|
} else { |
|
65
|
|
|
Basket::remove($itemIdentifier); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return redirect()->route('ecommerce.basket'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function inc(string $itemIdentifier): RedirectResponse |
|
75
|
|
|
{ |
|
76
|
|
|
/** @var ItemInterface $item */ |
|
77
|
|
|
if ($item = Basket::item($itemIdentifier)) { |
|
78
|
|
|
if ($item->quantity > 0) { |
|
79
|
|
|
++$item->quantity; |
|
80
|
|
|
} else { |
|
81
|
|
|
Basket::remove($itemIdentifier); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return redirect()->route('ecommerce.basket'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function dec(string $itemIdentifier): RedirectResponse |
|
89
|
|
|
{ |
|
90
|
|
|
/** @var ItemInterface $item */ |
|
91
|
|
|
if ($item = Basket::item($itemIdentifier)) { |
|
92
|
|
|
if ($item->quantity > 1) { |
|
93
|
|
|
--$item->quantity; |
|
94
|
|
|
} else { |
|
95
|
|
|
Basket::remove($itemIdentifier); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return redirect()->route('ecommerce.basket'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function remove(string $itemIdentifier): RedirectResponse |
|
103
|
|
|
{ |
|
104
|
|
|
if (Basket::item($itemIdentifier)) { |
|
105
|
|
|
Basket::remove($itemIdentifier); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return redirect()->route('ecommerce.basket'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function demo(): RedirectResponse |
|
112
|
|
|
{ |
|
113
|
|
|
$item = [ |
|
114
|
|
|
'id' => 1, |
|
115
|
|
|
'number' => 'zxy', |
|
116
|
|
|
'name' => 'My product', |
|
117
|
|
|
'stock' => 'In stock', |
|
118
|
|
|
'unit' => 'M', |
|
119
|
|
|
'tax' => 25, |
|
120
|
|
|
'price' => 100, |
|
121
|
|
|
'weight' => 100, |
|
122
|
|
|
'quantity' => 1, |
|
123
|
|
|
'type' => 'item', |
|
124
|
|
|
'link' => '', |
|
125
|
|
|
]; |
|
126
|
|
|
|
|
127
|
|
|
Basket::insert(new Item($item)); |
|
128
|
|
|
|
|
129
|
|
|
return redirect()->route('ecommerce.basket'); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|