Skip to content

Commit

Permalink
adding discounts to items
Browse files Browse the repository at this point in the history
  • Loading branch information
lukepolo committed Nov 6, 2015
1 parent 8ff43c2 commit 510163b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
22 changes: 10 additions & 12 deletions src/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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)
{
Expand Down
37 changes: 33 additions & 4 deletions src/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}

}
}
3 changes: 1 addition & 2 deletions src/Traits/CouponTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
*
Expand Down

0 comments on commit 510163b

Please sign in to comment.