mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-22 23:25:23 +00:00
Manually merged https://github.com/Crinsane/LaravelShoppingcart/pull/502
Added more tests
This commit is contained in:
33
src/Cart.php
33
src/Cart.php
@@ -424,6 +424,33 @@ class Cart
|
||||
return $this->numberFormat($this->initialFloat(), $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total weight of the items in the cart.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function weightFloat()
|
||||
{
|
||||
$content = $this->getContent();
|
||||
$total = $content->reduce(function ($total, CartItem $cartItem) {
|
||||
return $total + ($cartItem->qty * $cartItem->weight);
|
||||
}, 0);
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total weight of the items in the cart.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function weight($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
return $this->numberFormat($this->weightFloat(), $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the cart content for a cart item matching the given search closure.
|
||||
*
|
||||
@@ -603,12 +630,12 @@ class Cart
|
||||
* @param mixed $identifier Identifier of the Cart to merge with.
|
||||
* @param bool $keepDiscount Keep the discount of the CartItems.
|
||||
* @param bool $keepTax Keep the tax of the CartItems.
|
||||
* @return void
|
||||
* @return bool
|
||||
*/
|
||||
public function merge( $identifier, $keepDiscount = false, $keepTax = false )
|
||||
{
|
||||
if( ! $this->storedCartWithIdentifierExists($identifier)) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
$stored = $this->getConnection()->table($this->getTableName())
|
||||
@@ -619,6 +646,8 @@ class Cart
|
||||
foreach ($storedContent as $cartItem) {
|
||||
$this->addCartItem($cartItem, $keepDiscount, $keepTax);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -43,6 +43,13 @@ class CartItem implements Arrayable, Jsonable
|
||||
*/
|
||||
public $price;
|
||||
|
||||
/**
|
||||
* The weight of the product.
|
||||
*
|
||||
* @var float
|
||||
*/
|
||||
public $weight;
|
||||
|
||||
/**
|
||||
* The options for this cart item.
|
||||
*
|
||||
@@ -79,7 +86,7 @@ class CartItem implements Arrayable, Jsonable
|
||||
* @param float $price
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct($id, $name, $price, array $options = [])
|
||||
public function __construct($id, $name, $price, $weight = 0, array $options = [])
|
||||
{
|
||||
if(empty($id)) {
|
||||
throw new \InvalidArgumentException('Please supply a valid identifier.');
|
||||
@@ -94,6 +101,7 @@ class CartItem implements Arrayable, Jsonable
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->price = floatval($price);
|
||||
$this->weight = floatval($weight);
|
||||
$this->options = new CartItemOptions($options);
|
||||
$this->rowId = $this->generateRowId($id, $options);
|
||||
}
|
||||
@@ -217,6 +225,19 @@ class CartItem implements Arrayable, Jsonable
|
||||
return $this->numberFormat($this->discountTotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted weight.
|
||||
*
|
||||
* @param int $decimals
|
||||
* @param string $decimalPoint
|
||||
* @param string $thousandSeperator
|
||||
* @return string
|
||||
*/
|
||||
public function weight($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
return $this->numberFormat($this->weight, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the quantity for this cart item.
|
||||
*
|
||||
@@ -256,6 +277,7 @@ class CartItem implements Arrayable, Jsonable
|
||||
$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->weight = array_get($attributes, 'weight', $this->weight);
|
||||
$this->priceTax = $this->price + $this->tax;
|
||||
$this->options = new CartItemOptions(array_get($attributes, 'options', $this->options));
|
||||
|
||||
@@ -345,6 +367,10 @@ class CartItem implements Arrayable, Jsonable
|
||||
return $this->discount * $this->qty;
|
||||
}
|
||||
|
||||
if($attribute === 'weightTotal') {
|
||||
return $this->qty * $this->weight;
|
||||
}
|
||||
|
||||
if($attribute === 'model' && isset($this->associatedModel)) {
|
||||
return with(new $this->associatedModel)->find($this->id);
|
||||
}
|
||||
@@ -361,7 +387,7 @@ class CartItem implements Arrayable, Jsonable
|
||||
*/
|
||||
public static function fromBuyable(Buyable $item, array $options = [])
|
||||
{
|
||||
return new self($item->getBuyableIdentifier($options), $item->getBuyableDescription($options), $item->getBuyablePrice($options), $options);
|
||||
return new self($item->getBuyableIdentifier($options), $item->getBuyableDescription($options), $item->getBuyablePrice($options), $item->getBuyableWeight($options), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -386,9 +412,9 @@ class CartItem implements Arrayable, Jsonable
|
||||
* @param array $options
|
||||
* @return \Gloudemans\Shoppingcart\CartItem
|
||||
*/
|
||||
public static function fromAttributes($id, $name, $price, array $options = [])
|
||||
public static function fromAttributes($id, $name, $price, $weight = 0, array $options = [])
|
||||
{
|
||||
return new self($id, $name, $price, $options);
|
||||
return new self($id, $name, $price, $weight, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -418,6 +444,7 @@ class CartItem implements Arrayable, Jsonable
|
||||
'name' => $this->name,
|
||||
'qty' => $this->qty,
|
||||
'price' => $this->price,
|
||||
'weight' => $this->weight,
|
||||
'options' => $this->options->toArray(),
|
||||
'discount' => $this->discount,
|
||||
'tax' => $this->tax,
|
||||
|
||||
@@ -24,4 +24,11 @@ interface Buyable
|
||||
* @return float
|
||||
*/
|
||||
public function getBuyablePrice($options = null);
|
||||
|
||||
/**
|
||||
* Get the weight of the Buyable item.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getBuyableWeight($options = null);
|
||||
}
|
||||
Reference in New Issue
Block a user