|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Framework\Services; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use O2System\Spl\Patterns\Structural\Repository\AbstractRepository; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Cart |
|
22
|
|
|
* |
|
23
|
|
|
* @package O2System\Framework\Services |
|
24
|
|
|
*/ |
|
25
|
|
|
class Cart extends AbstractRepository |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Cart::__construct |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct() |
|
31
|
|
|
{ |
|
32
|
|
|
if (empty($_SESSION[ 'o2system' ][ 'cart' ])) { |
|
33
|
|
|
$_SESSION[ 'o2system' ][ 'cart' ] = []; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$this->storage =& $_SESSION[ 'o2system' ][ 'cart' ]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// ------------------------------------------------------------------------ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Cart::add |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $item |
|
45
|
|
|
*/ |
|
46
|
|
|
public function add(array $item) |
|
47
|
|
|
{ |
|
48
|
|
|
$item = array_merge([ |
|
49
|
|
|
'id' => null, |
|
50
|
|
|
'sku' => null, |
|
51
|
|
|
'quantity' => 1, |
|
52
|
|
|
'price' => 0, |
|
53
|
|
|
'discount' => 0, |
|
54
|
|
|
'name' => null, |
|
55
|
|
|
'options' => [], |
|
56
|
|
|
], $item); |
|
57
|
|
|
|
|
58
|
|
|
// set sku |
|
59
|
|
|
$sku = empty($item[ 'sku' ]) ? $item[ 'id' ] : $item[ 'sku' ]; |
|
60
|
|
|
|
|
61
|
|
|
// set sub-total |
|
62
|
|
|
$item[ 'subTotal' ][ 'price' ] = $item[ 'price' ] * $item[ 'quantity' ]; |
|
63
|
|
|
$item[ 'subTotal' ][ 'discount' ] = 0; |
|
64
|
|
|
|
|
65
|
|
|
if (is_numeric($item[ 'discount' ])) { |
|
66
|
|
|
$item[ 'subTotal' ][ 'discount' ] = $item[ 'subTotal' ][ 'price' ] - $item[ 'discount' ]; |
|
67
|
|
|
} elseif (is_string($item[ 'discount' ]) && strpos($item[ 'discount' ], '+') !== false) { |
|
68
|
|
|
$discounts = explode('+', $item[ 'discount' ]); |
|
69
|
|
|
if (count($discounts)) { |
|
70
|
|
|
$item[ 'subTotal' ][ 'discount' ] = $item[ 'subTotal' ][ 'price' ] * (intval(reset($discounts)) / 100); |
|
71
|
|
|
foreach (array_slice($discounts, 1) as $discount) { |
|
72
|
|
|
$item[ 'subTotal' ][ 'discount' ] += $item[ 'subTotal' ][ 'discount' ] * (intval($discount) / 100); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} elseif (is_string($item[ 'discount' ]) && strpos($item[ 'discount' ], '%') !== false) { |
|
76
|
|
|
$item[ 'subTotal' ][ 'discount' ] = $item[ 'subTotal' ][ 'price' ] * (intval($item[ 'discount' ]) / 100); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$item[ 'subTotal' ][ 'weight' ] = $item[ 'weight' ] * $item[ 'quantity' ]; |
|
80
|
|
|
|
|
81
|
|
|
$this->storage[ $sku ] = $item; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// ------------------------------------------------------------------------ |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Cart::update |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $sku |
|
90
|
|
|
* @param array $item |
|
91
|
|
|
* |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
public function update($sku, array $item) |
|
95
|
|
|
{ |
|
96
|
|
|
if ($this->offsetExists($sku)) { |
|
97
|
|
|
$item = array_merge($this->offsetGet($sku), $item); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
// update sub-total |
|
100
|
|
|
$item[ 'subTotal' ][ 'price' ] = $item[ 'price' ] * $item[ 'quantity' ]; |
|
101
|
|
|
$item[ 'subTotal' ][ 'weight' ] = $item[ 'weight' ] * $item[ 'quantity' ]; |
|
102
|
|
|
|
|
103
|
|
|
$this->storage[ $sku ] = $item; |
|
104
|
|
|
|
|
105
|
|
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// ------------------------------------------------------------------------ |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Cart::getTotalWeight |
|
115
|
|
|
* |
|
116
|
|
|
* @return int |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getTotalWeight() |
|
119
|
|
|
{ |
|
120
|
|
|
$totalWeight = 0; |
|
121
|
|
|
|
|
122
|
|
|
if ($this->count()) { |
|
123
|
|
|
foreach ($this->storage as $id => $item) { |
|
124
|
|
|
if (isset($item[ 'subTotal' ][ 'weight' ])) { |
|
125
|
|
|
$totalWeight += (int)$item[ 'weight' ]; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $totalWeight; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// ------------------------------------------------------------------------ |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Cart::getTotalPrice |
|
137
|
|
|
* |
|
138
|
|
|
* @return int |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getTotalPrice() |
|
141
|
|
|
{ |
|
142
|
|
|
$totalPrice = 0; |
|
143
|
|
|
|
|
144
|
|
|
if ($this->count()) { |
|
145
|
|
|
foreach ($this->storage as $id => $item) { |
|
146
|
|
|
if (isset($item[ 'subTotal' ][ 'price' ])) { |
|
147
|
|
|
$totalPrice += (int)$item[ 'price' ]; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $totalPrice; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
// ------------------------------------------------------------------------ |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Card::destroy |
|
159
|
|
|
*/ |
|
160
|
|
|
public function destroy() |
|
161
|
|
|
{ |
|
162
|
|
|
unset($_SESSION[ 'o2system' ][ 'cart' ]); |
|
163
|
|
|
parent::destroy(); |
|
164
|
|
|
} |
|
165
|
|
|
} |