Skip to content

Commit aac25da

Browse files
authored
Merge pull request #2 from omotolaawokunle/master
Added discounts functionality
2 parents 4a6ee3a + 71c3947 commit aac25da

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/Cartie.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct()
2121
}
2222

2323
//add items to cart
24-
public function add($item = array())
24+
public function add($item = array(), $discount = 0)
2525
{
2626
// Was any cart data passed?...
2727
if (!is_array($item) || count($item) === 0) {
@@ -50,6 +50,9 @@ public function add($item = array())
5050
}
5151
$item['rowid'] = $rowid;
5252
$this->CartContents[$rowid] = $item;
53+
if ($discount > 0) {
54+
$this->addDiscount($rowid, $discount);
55+
}
5356
$this->save_cart();
5457
} else {
5558
die('Error , The cart array must contain a product ID, quantity, price, and name.');
@@ -104,7 +107,11 @@ protected function save_cart()
104107

105108
$this->CartContents['cart_total'] += ($val['price'] * $val['quantity']);
106109
$this->CartContents['total_items'] += $val['quantity'];
107-
$this->CartContents[$key]['subtotal'] = ($this->CartContents[$key]['price'] * $this->CartContents[$key]['quantity']);
110+
$subtotal = $this->CartContents[$key]['subtotal'] = ($this->CartContents[$key]['price'] * $this->CartContents[$key]['quantity']);
111+
if (isset($this->CartContents[$key]['discount'])) {
112+
$this->CartContents[$key]['subtotal'] = $subtotal - ($subtotal * ($this->CartContents[$key]['discount'] / 100));
113+
$this->CartContents[$key]['subtotalWithoutDiscount'] = $subtotal;
114+
}
108115
}
109116
if (count($this->CartContents) <= 2) {
110117
unset($_SESSION['Cartie']);
@@ -166,4 +173,11 @@ public function clear()
166173
$this->CartContents = array('cart_total' => 0, 'total_items' => 0);
167174
unset($_SESSION['Cartie']);
168175
}
176+
177+
protected function addDiscount($rowid, $discount)
178+
{
179+
if (isset($this->CartContents[$rowid])) {
180+
$this->CartContents[$rowid]['discount'] = $discount;
181+
}
182+
}
169183
}

0 commit comments

Comments
 (0)