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