From 510163bc1c6988386026ebc4133ff4d94e4c681b Mon Sep 17 00:00:00 2001 From: Luke Policinski Date: Fri, 6 Nov 2015 16:03:29 +0000 Subject: [PATCH] adding discounts to items --- src/Cart.php | 22 ++++++++++------------ src/CartItem.php | 37 +++++++++++++++++++++++++++++++++---- src/Traits/CouponTrait.php | 3 +-- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 02899de..b77e87b 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -289,13 +289,7 @@ public function destroyCart() */ public function total($formatted = true, $withDiscount = true) { - $total = $this->subTotal(true, false); - - if ($withDiscount) { - $total -= $this->getTotalDiscount(false); - } - - $total += $this->getFeeTotals(false); + $total = $this->subTotal(true, false, $withDiscount) + $this->getFeeTotals(false); if ($formatted) { return \LaraCart::formatMoney($total, $this->locale, $this->internationalFormat); @@ -313,7 +307,7 @@ public function total($formatted = true, $withDiscount = true) */ public function taxTotal($formatted = true) { - $totalTax = $this->total(false) - $this->subTotal(false, false) - $this->getFeeTotals(false); + $totalTax = $this->total(false, false) - $this->subTotal(false, false, false) - $this->getFeeTotals(false); if ($formatted) { return \LaraCart::formatMoney($totalTax, $this->locale, $this->internationalFormat); @@ -351,16 +345,18 @@ public function getFeeTotals($formatted = true) /** * Gets the subtotal of the cart with or without tax * - * @param bool $tax + * @param bool|false $tax + * @param bool|true $formatted + * @param bool|true $withDiscount * * @return string */ - public function subTotal($tax = false, $formatted = true) + public function subTotal($tax = false, $formatted = true, $withDiscount = true) { $total = 0; if ($this->count() != 0) { foreach ($this->getItems() as $item) { - $total += $item->subTotal($tax, false) + $item->subItemsTotal($tax, false); + $total += $item->subTotal($tax, false, $withDiscount); } } @@ -395,7 +391,9 @@ public function count($withQty = true) /** * Gets the total amount discounted * - * @return int + * @param bool|true $formatted + * + * @return int|string */ public function getTotalDiscount($formatted = true) { diff --git a/src/CartItem.php b/src/CartItem.php index 21e6c82..c86efe1 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -237,21 +237,33 @@ public function update($key, $value) * * @param bool $tax * @param bool $format + * @param bool $withDiscount * * @return float|string */ - public function subTotal($tax = false, $format = true) + public function subTotal($tax = false, $format = true, $withDiscount = true) { + $total = ($this->getPrice($tax, false) + $this->subItemsTotal($tax, false)) * $this->qty; + if ($withDiscount) { + $total -= $this->getDiscount(false); + } + if ($format) { - $total = $this->getPrice($tax, false) + $this->subItemsTotal($tax, false); - return \LaraCart::formatMoney($total * $this->qty, $this->locale, $this->internationalFormat); + + return \LaraCart::formatMoney($total, $this->locale, $this->internationalFormat); } else { - return $this->getPrice($tax, false) * $this->qty; + return $total ; } } + /** * Gets the totals for the options + * + * @param bool|false $tax + * @param bool|true $format + * + * @return int|mixed|string */ public function subItemsTotal($tax = false, $format = true) { @@ -272,4 +284,21 @@ public function subItemsTotal($tax = false, $format = true) return $total; } } + + /** + * Gets the discount of an item + * + * @param bool|true $format + * + * @return mixed|null|string + */ + public function getDiscount($format = true) + { + if($format) { + return \LaraCart::formatMoney($this->discount, $this->locale, $this->internationalFormat); + } else { + return $this->discount; + } + + } } diff --git a/src/Traits/CouponTrait.php b/src/Traits/CouponTrait.php index efa10f8..b1c9ba3 100644 --- a/src/Traits/CouponTrait.php +++ b/src/Traits/CouponTrait.php @@ -133,7 +133,6 @@ public function maxDiscount($maxDiscount, $discount, $throwErrors) */ public function checkValidTimes(Carbon $startDate, Carbon $endDate, $throwErrors) { - // TODO - remove coupon from the cart if (Carbon::now()->between($startDate, $endDate)) { return true; } else { @@ -144,7 +143,7 @@ public function checkValidTimes(Carbon $startDate, Carbon $endDate, $throwErrors } } } - + /** * Sets a discount to an item with what code was used and the discount amount *