Skip to content

Commit 262f1e8

Browse files
extra costs as collection
1 parent 98d23aa commit 262f1e8

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/Cart.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class Cart
4040
*/
4141
private $instance;
4242

43+
/**
44+
* Holds the extra additional costs on the cart
45+
*
46+
* @var Collection
47+
*/
4348
private $extraCosts;
4449

4550
/**
@@ -52,7 +57,7 @@ public function __construct(SessionManager $session, Dispatcher $events)
5257
{
5358
$this->session = $session;
5459
$this->events = $events;
55-
$this->extraCosts = array();
60+
$this->extraCosts = new Collection();
5661

5762
$this->instance(self::DEFAULT_INSTANCE);
5863
}
@@ -126,24 +131,23 @@ public function add($id, $name = null, $qty = null, $price = null, array $option
126131
*/
127132
public function setCost($name, $price)
128133
{
129-
$this->extraCosts[$name] = $price;
134+
$oldCost = $this->extraCosts->pull($name, 0);
135+
136+
$this->extraCosts->put($name, $price + $oldCost);
130137
}
131138

132139
/**
133140
* Gets an additional cost by name
134141
*
135142
* @param $name
136-
* @param null $decimals
137-
* @param null $decimalPoint
138-
* @param null $thousandSeperator
143+
* @param int|null $decimals
144+
* @param string|null $decimalPoint
145+
* @param string|null $thousandSeperator
139146
* @return string
140147
*/
141148
public function getCost($name, $decimals = null, $decimalPoint = null, $thousandSeperator = null)
142149
{
143-
$cost = 0;
144-
145-
if(isset($this->extraCosts[$name]))
146-
$cost = $this->extraCosts[$name];
150+
$cost = $this->extraCosts->get($name, 0);
147151

148152
return $this->numberFormat($cost, $decimals, $decimalPoint, $thousandSeperator);
149153
}
@@ -240,12 +244,12 @@ public function destroy()
240244
/**
241245
* Get the content of the cart.
242246
*
243-
* @return \Illuminate\Support\Collection
247+
* @return Collection
244248
*/
245249
public function content()
246250
{
247251
if (is_null($this->session->get($this->instance))) {
248-
return new Collection([]);
252+
return new Collection();
249253
}
250254

251255
return $this->session->get($this->instance);
@@ -279,9 +283,11 @@ public function total($decimals = null, $decimalPoint = null, $thousandSeperator
279283
return $total + ($cartItem->qty * $cartItem->priceTax);
280284
}, 0);
281285

282-
foreach ($this->extraCosts as $cost) {
283-
$total += $cost;
284-
}
286+
$totalCost = $this->extraCosts->reduce(function ($total, $cost) {
287+
return $total + $cost;
288+
}, 0);
289+
290+
$total += $totalCost;
285291

286292
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
287293
}
@@ -328,7 +334,7 @@ public function subtotal($decimals = null, $decimalPoint = null, $thousandSepera
328334
* Search the cart content for a cart item matching the given search closure.
329335
*
330336
* @param \Closure $search
331-
* @return \Illuminate\Support\Collection
337+
* @return Collection
332338
*/
333339
public function search(Closure $search)
334340
{
@@ -467,7 +473,7 @@ public function __get($attribute)
467473
/**
468474
* Get the carts content, if there is no cart content set yet, return a new empty Collection
469475
*
470-
* @return \Illuminate\Support\Collection
476+
* @return Collection
471477
*/
472478
protected function getContent()
473479
{

0 commit comments

Comments
 (0)