Skip to content

Commit

Permalink
Merge pull request #2 from omotolaawokunle/master
Browse files Browse the repository at this point in the history
Added discounts functionality
  • Loading branch information
LPMatrix authored Sep 23, 2020
2 parents 4a6ee3a + 71c3947 commit aac25da
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Cartie.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct()
}

//add items to cart
public function add($item = array())
public function add($item = array(), $discount = 0)
{
// Was any cart data passed?...
if (!is_array($item) || count($item) === 0) {
Expand Down Expand Up @@ -50,6 +50,9 @@ public function add($item = array())
}
$item['rowid'] = $rowid;
$this->CartContents[$rowid] = $item;
if ($discount > 0) {
$this->addDiscount($rowid, $discount);
}
$this->save_cart();
} else {
die('Error , The cart array must contain a product ID, quantity, price, and name.');
Expand Down Expand Up @@ -104,7 +107,11 @@ protected function save_cart()

$this->CartContents['cart_total'] += ($val['price'] * $val['quantity']);
$this->CartContents['total_items'] += $val['quantity'];
$this->CartContents[$key]['subtotal'] = ($this->CartContents[$key]['price'] * $this->CartContents[$key]['quantity']);
$subtotal = $this->CartContents[$key]['subtotal'] = ($this->CartContents[$key]['price'] * $this->CartContents[$key]['quantity']);
if (isset($this->CartContents[$key]['discount'])) {
$this->CartContents[$key]['subtotal'] = $subtotal - ($subtotal * ($this->CartContents[$key]['discount'] / 100));
$this->CartContents[$key]['subtotalWithoutDiscount'] = $subtotal;
}
}
if (count($this->CartContents) <= 2) {
unset($_SESSION['Cartie']);
Expand Down Expand Up @@ -166,4 +173,11 @@ public function clear()
$this->CartContents = array('cart_total' => 0, 'total_items' => 0);
unset($_SESSION['Cartie']);
}

protected function addDiscount($rowid, $discount)
{
if (isset($this->CartContents[$rowid])) {
$this->CartContents[$rowid]['discount'] = $discount;
}
}
}

0 comments on commit aac25da

Please sign in to comment.