mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-22 23:25:23 +00:00
Adjusted for global discount
Fixed tests for Laravel 5.7
This commit is contained in:
@@ -64,6 +64,13 @@ class CartItem implements Arrayable, Jsonable
|
||||
*/
|
||||
private $taxRate = 0;
|
||||
|
||||
/**
|
||||
* The discount rate for the cart item.
|
||||
*
|
||||
* @var float
|
||||
*/
|
||||
private $discountRate = 0;
|
||||
|
||||
/**
|
||||
* CartItem constructor.
|
||||
*
|
||||
@@ -84,11 +91,11 @@ class CartItem implements Arrayable, Jsonable
|
||||
throw new \InvalidArgumentException('Please supply a valid price.');
|
||||
}
|
||||
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->price = floatval($price);
|
||||
$this->options = new CartItemOptions($options);
|
||||
$this->rowId = $this->generateRowId($id, $options);
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->price = floatval($price);
|
||||
$this->options = new CartItemOptions($options);
|
||||
$this->rowId = $this->generateRowId($id, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,6 +110,19 @@ class CartItem implements Arrayable, Jsonable
|
||||
{
|
||||
return $this->numberFormat($this->price, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted price with discount applied.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function priceTarget($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
return $this->numberFormat($this->priceTarget, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted price with TAX.
|
||||
@@ -171,6 +191,32 @@ class CartItem implements Arrayable, Jsonable
|
||||
return $this->numberFormat($this->taxTotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted tax.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function discount($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
return $this->numberFormat($this->discount, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted tax.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function discountTotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
return $this->numberFormat($this->discountTotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the quantity for this cart item.
|
||||
*
|
||||
@@ -242,6 +288,19 @@ class CartItem implements Arrayable, Jsonable
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the discount rate.
|
||||
*
|
||||
* @param int|float $discountRate
|
||||
* @return \Gloudemans\Shoppingcart\CartItem
|
||||
*/
|
||||
public function setDiscountRate($discountRate)
|
||||
{
|
||||
$this->discountRate = $discountRate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an attribute from the cart item or get the associated model.
|
||||
*
|
||||
@@ -254,26 +313,38 @@ class CartItem implements Arrayable, Jsonable
|
||||
return $this->{$attribute};
|
||||
}
|
||||
|
||||
if($attribute === 'priceTax') {
|
||||
return $this->price + $this->tax;
|
||||
if($attribute === 'discount') {
|
||||
return $this->price * ($this->discountRate / 100);
|
||||
}
|
||||
|
||||
|
||||
if($attribute === 'priceTarget') {
|
||||
return $this->price - $this->discount;
|
||||
}
|
||||
|
||||
if($attribute === 'subtotal') {
|
||||
return $this->qty * $this->price;
|
||||
return $this->qty * $this->priceTarget;
|
||||
}
|
||||
|
||||
if($attribute === 'tax') {
|
||||
return $this->priceTarget * ($this->taxRate / 100);
|
||||
}
|
||||
|
||||
if($attribute === 'priceTax') {
|
||||
return $this->priceTarget + $this->tax;
|
||||
}
|
||||
|
||||
if($attribute === 'total') {
|
||||
return $this->qty * ($this->priceTax);
|
||||
}
|
||||
|
||||
if($attribute === 'tax') {
|
||||
return $this->price * ($this->taxRate / 100);
|
||||
}
|
||||
|
||||
if($attribute === 'taxTotal') {
|
||||
return $this->tax * $this->qty;
|
||||
}
|
||||
|
||||
if($attribute === 'discountTotal') {
|
||||
return $this->discount * $this->qty;
|
||||
}
|
||||
|
||||
if($attribute === 'model' && isset($this->associatedModel)) {
|
||||
return with(new $this->associatedModel)->find($this->id);
|
||||
}
|
||||
@@ -348,6 +419,7 @@ class CartItem implements Arrayable, Jsonable
|
||||
'qty' => $this->qty,
|
||||
'price' => $this->price,
|
||||
'options' => $this->options->toArray(),
|
||||
'discount' => $this->discount,
|
||||
'tax' => $this->tax,
|
||||
'subtotal' => $this->subtotal
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user