Skip to content

Commit f065a7b

Browse files
Possibility to add extra costs
- eg shipping, transaction, ... - dirty fix - i know, need it urgently
1 parent 96157e0 commit f065a7b

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,11 @@ Below is a little example of how to list the cart content in a table:
394394
Cart::add('192ao12', 'Product 1', 1, 9.99);
395395
Cart::add('1239ad0', 'Product 2', 2, 5.95, ['size' => 'large']);
396396

397+
// Set an additional cost (on the same page where you display your cart content)
398+
Cart::setCost(Cart::COST_TRANSACTION, 0.10);
399+
Cart::setCost(Cart::COST_SHIPPING, 5.00);
400+
Cart::setCost('somethingelse', 1.11);
401+
397402
// Display the content in a View.
398403
<table>
399404
<thead>
@@ -434,6 +439,21 @@ Cart::add('1239ad0', 'Product 2', 2, 5.95, ['size' => 'large']);
434439
<td>Tax</td>
435440
<td><?php echo Cart::tax(); ?></td>
436441
</tr>
442+
<tr>
443+
<td colspan="2">&nbsp;</td>
444+
<td>Transaction cost</td>
445+
<td><?php echo Cart::getCost(\Gloudemans\Shoppingcart\Cart::COST_TRANSACTION); ?></td>
446+
</tr>
447+
<tr>
448+
<td colspan="2">&nbsp;</td>
449+
<td>Transaction cost</td>
450+
<td><?php echo Cart::getCost(\Gloudemans\Shoppingcart\Cart::COST_SHIPPING); ?></td>
451+
</tr>
452+
<tr>
453+
<td colspan="2">&nbsp;</td>
454+
<td>Transaction cost</td>
455+
<td><?php echo Cart::getCost('somethingelse'); ?></td>
456+
</tr>
437457
<tr>
438458
<td colspan="2">&nbsp;</td>
439459
<td>Total</td>

src/Cart.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class Cart
1616
{
1717
const DEFAULT_INSTANCE = 'default';
1818

19+
const COST_SHIPPING = 'shipping';
20+
const COST_TRANSACTION = 'transaction';
21+
1922
/**
2023
* Instance of the session manager.
2124
*
@@ -37,6 +40,8 @@ class Cart
3740
*/
3841
private $instance;
3942

43+
private $extraCosts;
44+
4045
/**
4146
* Cart constructor.
4247
*
@@ -47,6 +52,7 @@ public function __construct(SessionManager $session, Dispatcher $events)
4752
{
4853
$this->session = $session;
4954
$this->events = $events;
55+
$this->extraCosts = array();
5056

5157
$this->instance(self::DEFAULT_INSTANCE);
5258
}
@@ -111,6 +117,30 @@ public function add($id, $name = null, $qty = null, $price = null, array $option
111117
return $cartItem;
112118
}
113119

120+
/**
121+
* Sets/adds an additional cost on the cart.
122+
*
123+
* @param $type
124+
* @param $price
125+
* @todo add in session
126+
*/
127+
public function setCost($name, $price)
128+
{
129+
$this->extraCosts[$name] = $price;
130+
}
131+
132+
/**
133+
* @param $name
134+
* @param null $decimals
135+
* @param null $decimalPoint
136+
* @param null $thousandSeperator
137+
* @return string
138+
*/
139+
public function getCost($name, $decimals = null, $decimalPoint = null, $thousandSeperator = null)
140+
{
141+
return $this->numberFormat($this->extraCosts[$name] ?? 0, $decimals, $decimalPoint, $thousandSeperator);
142+
}
143+
114144
/**
115145
* Update the cart item with the given rowId.
116146
*
@@ -242,6 +272,10 @@ public function total($decimals = null, $decimalPoint = null, $thousandSeperator
242272
return $total + ($cartItem->qty * $cartItem->priceTax);
243273
}, 0);
244274

275+
foreach ($this->extraCosts as $cost) {
276+
$total += $cost;
277+
}
278+
245279
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
246280
}
247281

0 commit comments

Comments
 (0)