mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-24 07:55:35 +00:00
Fixed cart calculation methods
This commit is contained in:
44
src/Cart.php
44
src/Cart.php
@@ -253,7 +253,7 @@ class Cart
|
|||||||
$content = $this->getContent();
|
$content = $this->getContent();
|
||||||
|
|
||||||
$total = $content->reduce(function ($total, CartItem $cartItem) {
|
$total = $content->reduce(function ($total, CartItem $cartItem) {
|
||||||
return $total + ($cartItem->qty * $cartItem->priceTax);
|
return $total + $cartItem->total;
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
||||||
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
|
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
|
||||||
@@ -272,7 +272,7 @@ class Cart
|
|||||||
$content = $this->getContent();
|
$content = $this->getContent();
|
||||||
|
|
||||||
$tax = $content->reduce(function ($tax, CartItem $cartItem) {
|
$tax = $content->reduce(function ($tax, CartItem $cartItem) {
|
||||||
return $tax + ($cartItem->qty * $cartItem->tax);
|
return $tax + $cartItem->taxTotal;
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
||||||
return $this->numberFormat($tax, $decimals, $decimalPoint, $thousandSeperator);
|
return $this->numberFormat($tax, $decimals, $decimalPoint, $thousandSeperator);
|
||||||
@@ -291,12 +291,50 @@ class Cart
|
|||||||
$content = $this->getContent();
|
$content = $this->getContent();
|
||||||
|
|
||||||
$subTotal = $content->reduce(function ($subTotal, CartItem $cartItem) {
|
$subTotal = $content->reduce(function ($subTotal, CartItem $cartItem) {
|
||||||
return $subTotal + ($cartItem->qty * $cartItem->price);
|
return $subTotal + $cartItem->subtotal;
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
||||||
return $this->numberFormat($subTotal, $decimals, $decimalPoint, $thousandSeperator);
|
return $this->numberFormat($subTotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the subtotal (total - tax) of the items in the cart.
|
||||||
|
*
|
||||||
|
* @param int $decimals
|
||||||
|
* @param string $decimalPoint
|
||||||
|
* @param string $thousandSeperator
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function discount($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||||
|
{
|
||||||
|
$content = $this->getContent();
|
||||||
|
|
||||||
|
$discount = $content->reduce(function ($discount, CartItem $cartItem) {
|
||||||
|
return $discount + $cartItem->discountTotal;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
return $this->numberFormat($discount, $decimals, $decimalPoint, $thousandSeperator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the subtotal (total - tax) of the items in the cart.
|
||||||
|
*
|
||||||
|
* @param int $decimals
|
||||||
|
* @param string $decimalPoint
|
||||||
|
* @param string $thousandSeperator
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function initial($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||||
|
{
|
||||||
|
$content = $this->getContent();
|
||||||
|
|
||||||
|
$initial = $content->reduce(function ($initial, CartItem $cartItem) {
|
||||||
|
return $initial + ($cartItem->qty * $cartItem->price);
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
return $this->numberFormat($initial, $decimals, $decimalPoint, $thousandSeperator);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search the cart content for a cart item matching the given search closure.
|
* Search the cart content for a cart item matching the given search closure.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -334,7 +334,7 @@ class CartItem implements Arrayable, Jsonable
|
|||||||
}
|
}
|
||||||
|
|
||||||
if($attribute === 'total') {
|
if($attribute === 'total') {
|
||||||
return $this->qty * ($this->priceTax);
|
return $this->qty * $this->priceTax;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($attribute === 'taxTotal') {
|
if($attribute === 'taxTotal') {
|
||||||
|
|||||||
Reference in New Issue
Block a user