mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-15 12:24:51 +00:00
Updated: Tax issues + Added methods for CartItem
This commit is contained in:
15
src/Cart.php
15
src/Cart.php
@@ -236,8 +236,8 @@ class Cart
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$total = $content->reduce(function ($subTotal, CartItem $cartItem) {
|
||||
return $subTotal + ($cartItem->qty * $cartItem->price);
|
||||
$total = $content->reduce(function ($total, CartItem $cartItem) {
|
||||
return $total + ($cartItem->qty * $cartItem->priceTax);
|
||||
}, 0);
|
||||
|
||||
return number_format($total, $decimals, $decimalPoint, $thousandSeperator);
|
||||
@@ -272,9 +272,13 @@ class Cart
|
||||
*/
|
||||
public function subtotal($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
|
||||
{
|
||||
$subtotal = $this->total - $this->tax;
|
||||
$content = $this->getContent();
|
||||
|
||||
return number_format($subtotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
$subTotal = $content->reduce(function ($subTotal, CartItem $cartItem) {
|
||||
return $subTotal + ($cartItem->qty * $cartItem->price);
|
||||
}, 0);
|
||||
|
||||
return number_format($subTotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -303,8 +307,9 @@ class Cart
|
||||
*/
|
||||
public function associate($rowId, $model)
|
||||
{
|
||||
if(is_string($model) && ! class_exists($model))
|
||||
if(is_string($model) && ! class_exists($model)) {
|
||||
throw new UnknownModelException("The supplied model {$model} does not exist.");
|
||||
}
|
||||
|
||||
$cartItem = $this->get($rowId);
|
||||
|
||||
|
||||
@@ -36,11 +36,12 @@ class CartItem implements Arrayable
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* The price of the cart item.
|
||||
* The price without TAX of the cart item.
|
||||
*
|
||||
* @var float
|
||||
*/
|
||||
public $price;
|
||||
|
||||
|
||||
/**
|
||||
* The options for this cart item.
|
||||
@@ -73,21 +74,25 @@ class CartItem implements Arrayable
|
||||
*/
|
||||
public function __construct($id, $name, $price, array $options = [])
|
||||
{
|
||||
if(empty($id)) throw new \InvalidArgumentException('Please supply a valid identifier.');
|
||||
if(empty($name)) throw new \InvalidArgumentException('Please supply a valid name.');
|
||||
if(strlen($price) < 0 || ! is_numeric($price))
|
||||
if(empty($id)) {
|
||||
throw new \InvalidArgumentException('Please supply a valid identifier.');
|
||||
}
|
||||
if(empty($name)) {
|
||||
throw new \InvalidArgumentException('Please supply a valid name.');
|
||||
}
|
||||
if(strlen($price) < 0 || ! is_numeric($price)) {
|
||||
throw new \InvalidArgumentException('Please supply a valid price.');
|
||||
}
|
||||
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->price = $price;
|
||||
$this->options = new CartItemOptions($options);
|
||||
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->price = floatval($price);
|
||||
$this->options = new CartItemOptions($options);
|
||||
$this->rowId = $this->generateRowId($id, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted price.
|
||||
* Returns the formatted price without TAX.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
@@ -98,9 +103,23 @@ class CartItem implements Arrayable
|
||||
{
|
||||
return number_format($this->price, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted price with TAX.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function priceTax($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
|
||||
{
|
||||
return number_format($this->priceTax, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted subtotal.
|
||||
* Subtotal is price for whole CartItem without TAX
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
@@ -111,6 +130,20 @@ class CartItem implements Arrayable
|
||||
{
|
||||
return number_format($this->subtotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted total.
|
||||
* Total is price for whole CartItem with TAX
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function total($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
|
||||
{
|
||||
return number_format($this->total, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted tax.
|
||||
@@ -124,6 +157,19 @@ class CartItem implements Arrayable
|
||||
{
|
||||
return number_format($this->tax, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted tax.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function taxTotal($decimals = 2, $decimalPoint = '.', $thousandSeperator = ',')
|
||||
{
|
||||
return number_format(($this->tax * $this->qty), $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the quantity for this cart item.
|
||||
@@ -146,9 +192,10 @@ class CartItem implements Arrayable
|
||||
*/
|
||||
public function updateFromBuyable(Buyable $item)
|
||||
{
|
||||
$this->id = $item->getBuyableIdentifier();
|
||||
$this->name = $item->getBuyableDescription();
|
||||
$this->price = $item->getBuyablePrice();
|
||||
$this->id = $item->getBuyableIdentifier();
|
||||
$this->name = $item->getBuyableDescription();
|
||||
$this->price = $item->getBuyablePrice();
|
||||
$this->priceTax = $this->price + $this->tax;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,11 +206,12 @@ class CartItem implements Arrayable
|
||||
*/
|
||||
public function updateFromArray(array $attributes)
|
||||
{
|
||||
$this->id = array_get($attributes, 'id', $this->id);
|
||||
$this->qty = array_get($attributes, 'qty', $this->qty);
|
||||
$this->name = array_get($attributes, 'name', $this->name);
|
||||
$this->price = array_get($attributes, 'price', $this->price);
|
||||
$this->options = new CartItemOptions(array_get($attributes, 'options', []));
|
||||
$this->id = array_get($attributes, 'id', $this->id);
|
||||
$this->qty = array_get($attributes, 'qty', $this->qty);
|
||||
$this->name = array_get($attributes, 'name', $this->name);
|
||||
$this->price = array_get($attributes, 'price', $this->price);
|
||||
$this->priceTax = $this->price + $this->tax;
|
||||
$this->options = new CartItemOptions(array_get($attributes, 'options', []));
|
||||
|
||||
$this->rowId = $this->generateRowId($this->id, $this->options->all());
|
||||
}
|
||||
@@ -202,9 +250,17 @@ class CartItem implements Arrayable
|
||||
return $this->{$attribute};
|
||||
}
|
||||
|
||||
if($attribute === 'priceTax') {
|
||||
return $this->price + $this->tax;
|
||||
}
|
||||
|
||||
if($attribute === 'subtotal') {
|
||||
return $this->qty * $this->price;
|
||||
}
|
||||
|
||||
if($attribute === 'total') {
|
||||
return $this->qty * ($this->priceTax);
|
||||
}
|
||||
|
||||
if($attribute === 'tax') {
|
||||
return $this->price * ($this->taxRate / 100);
|
||||
|
||||
Reference in New Issue
Block a user