|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LukePOLO\LaraCart; |
|
4
|
|
|
|
|
5
|
|
|
use LukePOLO\LaraCart\Exceptions\ModelNotFound; |
|
6
|
|
|
use LukePOLO\LaraCart\Traits\CartOptionsMagicMethodsTrait; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class CartItem |
|
10
|
|
|
* |
|
11
|
|
|
* @package LukePOLO\LaraCart |
|
12
|
|
|
*/ |
|
13
|
|
|
class CartItem |
|
14
|
|
|
{ |
|
15
|
|
|
use CartOptionsMagicMethodsTrait; |
|
16
|
|
|
|
|
17
|
|
|
protected $itemHash; |
|
18
|
|
|
protected $itemModel; |
|
19
|
|
|
|
|
20
|
|
|
public $locale; |
|
21
|
|
|
public $taxable; |
|
22
|
|
|
public $lineItem; |
|
23
|
|
|
public $subItems = []; |
|
24
|
|
|
public $couponInfo = []; |
|
25
|
|
|
public $internationalFormat; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* CartItem constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param $id |
|
31
|
|
|
* @param $name |
|
32
|
|
|
* @param integer $qty |
|
33
|
|
|
* @param string $price |
|
34
|
|
|
* @param array $options |
|
35
|
|
|
* @param boolean $taxable |
|
36
|
|
|
* @param bool|false $lineItem |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct($id, $name, $qty, $price, $options = [], $taxable = true, $lineItem = false) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->id = $id; |
|
|
|
|
|
|
41
|
|
|
$this->qty = $qty; |
|
|
|
|
|
|
42
|
|
|
$this->name = $name; |
|
|
|
|
|
|
43
|
|
|
$this->taxable = $taxable; |
|
44
|
|
|
$this->lineItem = $lineItem; |
|
45
|
|
|
$this->price = floatval($price); |
|
|
|
|
|
|
46
|
|
|
$this->tax = config('laracart.tax'); |
|
|
|
|
|
|
47
|
|
|
$this->itemModel = config('laracart.item_model'); |
|
48
|
|
|
|
|
49
|
|
|
foreach ($options as $option => $value) { |
|
50
|
|
|
$this->$option = $value; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Generates a hash based on the cartItem array |
|
56
|
|
|
* |
|
57
|
|
|
* @param bool $force |
|
58
|
|
|
* |
|
59
|
|
|
* @return string itemHash |
|
60
|
|
|
*/ |
|
61
|
|
|
public function generateHash($force = false) |
|
62
|
|
|
{ |
|
63
|
|
|
if ($this->lineItem === false) { |
|
64
|
|
|
$this->itemHash = null; |
|
65
|
|
|
|
|
66
|
|
|
$cartItemArray = (array)$this; |
|
67
|
|
|
|
|
68
|
|
|
unset($cartItemArray['options']['qty']); |
|
69
|
|
|
|
|
70
|
|
|
ksort($cartItemArray['options']); |
|
71
|
|
|
|
|
72
|
|
|
$this->itemHash = app(LaraCart::HASH, $cartItemArray); |
|
73
|
|
|
} elseif ($force || empty($this->itemHash) === true) { |
|
74
|
|
|
$this->itemHash = app(LaraCart::RANHASH); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
app('events')->fire( |
|
78
|
|
|
'laracart.updateItem', [ |
|
79
|
|
|
'item' => $this, |
|
80
|
|
|
'newHash' => $this->itemHash |
|
81
|
|
|
] |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
return $this->itemHash; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Gets the hash for the item |
|
89
|
|
|
* |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getHash() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->itemHash; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Finds a sub item by its hash |
|
99
|
|
|
* |
|
100
|
|
|
* @param $subItemHash |
|
101
|
|
|
* @return mixed |
|
102
|
|
|
*/ |
|
103
|
|
|
public function findSubItem($subItemHash) |
|
104
|
|
|
{ |
|
105
|
|
|
return array_get($this->subItems, $subItemHash); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Adds an sub item to a item |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $subItem |
|
112
|
|
|
* |
|
113
|
|
|
* @return CartSubItem $itemHash |
|
114
|
|
|
*/ |
|
115
|
|
|
public function addSubItem(array $subItem) |
|
116
|
|
|
{ |
|
117
|
|
|
$subItem = new CartSubItem($subItem); |
|
118
|
|
|
|
|
119
|
|
|
$this->subItems[$subItem->getHash()] = $subItem; |
|
120
|
|
|
|
|
121
|
|
|
$this->generateHash(); |
|
122
|
|
|
|
|
123
|
|
|
return $subItem; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Removes a sub item from the item |
|
128
|
|
|
* |
|
129
|
|
|
* @param $subItemHash |
|
130
|
|
|
*/ |
|
131
|
|
|
public function removeSubItem($subItemHash) |
|
132
|
|
|
{ |
|
133
|
|
|
unset($this->subItems[$subItemHash]); |
|
134
|
|
|
|
|
135
|
|
|
$this->generateHash(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Gets the price of the item with or without tax, with the proper format |
|
140
|
|
|
* |
|
141
|
|
|
* @param bool $format |
|
142
|
|
|
* |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
public function price($format = true) |
|
146
|
|
|
{ |
|
147
|
|
|
return LaraCart::formatMoney( |
|
148
|
|
|
$this->price + $this->subItemsTotal(false), |
|
|
|
|
|
|
149
|
|
|
$this->locale, |
|
150
|
|
|
$this->internationalFormat, $format |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Gets the sub total of the item based on the qty with or without tax in the proper format |
|
156
|
|
|
* |
|
157
|
|
|
* @param bool $format |
|
158
|
|
|
* @param bool $withDiscount |
|
159
|
|
|
* |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
|
|
public function subTotal($format = true, $withDiscount = true) |
|
163
|
|
|
{ |
|
164
|
|
|
$total = $this->price(false) * $this->qty; |
|
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
if ($withDiscount) { |
|
167
|
|
|
$total -= $this->getDiscount(false); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return LaraCart::formatMoney($total, $this->locale, $this->internationalFormat, $format); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Gets the totals for the options |
|
176
|
|
|
* |
|
177
|
|
|
* @param boolean $format |
|
178
|
|
|
* |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
public function subItemsTotal($format = true) |
|
182
|
|
|
{ |
|
183
|
|
|
$total = 0; |
|
184
|
|
|
|
|
185
|
|
|
foreach ($this->subItems as $subItem) { |
|
186
|
|
|
$total += $subItem->price(false); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$total *= $this->qty; |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
return LaraCart::formatMoney($total, $this->locale, $this->internationalFormat, $format); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Gets the discount of an item |
|
196
|
|
|
* |
|
197
|
|
|
* @param boolean $format |
|
198
|
|
|
* |
|
199
|
|
|
* @return string |
|
200
|
|
|
*/ |
|
201
|
|
|
public function getDiscount($format = true) |
|
202
|
|
|
{ |
|
203
|
|
|
return LaraCart::formatMoney( |
|
204
|
|
|
$this->discount, |
|
|
|
|
|
|
205
|
|
|
$this->locale, |
|
206
|
|
|
$this->internationalFormat, |
|
207
|
|
|
$format |
|
208
|
|
|
); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Gets the tax for the item |
|
213
|
|
|
* |
|
214
|
|
|
* @return int|mixed |
|
215
|
|
|
*/ |
|
216
|
|
|
public function tax($amountNotTaxable = 0) |
|
217
|
|
|
{ |
|
218
|
|
|
$tax = 0; |
|
219
|
|
|
|
|
220
|
|
|
if ($this->taxable) { |
|
221
|
|
|
return $this->tax * ($this->subTotal(false) - $amountNotTaxable); |
|
|
|
|
|
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
return $tax; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Sets the related model to the item |
|
229
|
|
|
* |
|
230
|
|
|
* @param $itemModel |
|
231
|
|
|
* |
|
232
|
|
|
* @throws ModelNotFound |
|
233
|
|
|
*/ |
|
234
|
|
|
public function setModel($itemModel) |
|
235
|
|
|
{ |
|
236
|
|
|
if(!class_exists($itemModel)) { |
|
|
|
|
|
|
237
|
|
|
throw new ModelNotFound(); |
|
238
|
|
|
} |
|
239
|
|
|
$this->itemModel = $itemModel; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Returns a Model |
|
244
|
|
|
* |
|
245
|
|
|
* @throws |
|
246
|
|
|
*/ |
|
247
|
|
|
public function getModel() |
|
248
|
|
|
{ |
|
249
|
|
|
$itemModel = new $this->itemModel; |
|
250
|
|
|
|
|
251
|
|
|
return $itemModel->findOrFail($this->id); |
|
|
|
|
|
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.