mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-11 18:54:33 +00:00
manuall readded reverted changes
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
language: php
|
||||
|
||||
php:
|
||||
- 5.6
|
||||
- 7.0
|
||||
- 7.1
|
||||
- 7.2
|
||||
|
||||
before_script:
|
||||
|
||||
48
README.md
48
README.md
@@ -10,24 +10,12 @@ This is a fork of [Crisane's LaravelShoppingcart](https://github.com/Crinsane/La
|
||||
|
||||
## Installation
|
||||
|
||||
Install the package through [Composer](http://getcomposer.org/).
|
||||
Install the [package](https://packagist.org/packages/bumbummen99/shoppingcart) through [Composer](http://getcomposer.org/).
|
||||
|
||||
Run the Composer require command from the Terminal:
|
||||
|
||||
composer require bumbummen99/shoppingcart
|
||||
|
||||
If you're using Laravel 5.5, this is all there is to do.
|
||||
|
||||
Should you still be on version 5.4 of Laravel, the final steps for you are to add the service provider of the package and alias the package. To do this open your `config/app.php` file.
|
||||
|
||||
Add a new line to the `providers` array:
|
||||
|
||||
Gloudemans\Shoppingcart\ShoppingcartServiceProvider::class
|
||||
|
||||
And optionally add a new line to the `aliases` array:
|
||||
|
||||
'Cart' => Gloudemans\Shoppingcart\Facades\Cart::class,
|
||||
|
||||
Now you're ready to start using the shoppingcart in your application.
|
||||
|
||||
**As of version 2 of this package it's possibly to use dependency injection to inject an instance of the Cart class into your controller or other class**
|
||||
@@ -61,7 +49,7 @@ Cart::add('293ad', 'Product 1', 1, 9.99);
|
||||
As an optional fifth parameter you can pass it options, so you can add multiple items with the same id, but with (for instance) a different size.
|
||||
|
||||
```php
|
||||
Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']);
|
||||
Cart::add('293ad', 'Product 1', 1, 9.99, 'weight' => 550, ['size' => 'large']);
|
||||
```
|
||||
|
||||
**The `add()` method will return an CartItem instance of the item you just added to the cart.**
|
||||
@@ -69,7 +57,7 @@ Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']);
|
||||
Maybe you prefer to add the item using an array? As long as the array contains the required keys, you can pass it to the method. The options key is optional.
|
||||
|
||||
```php
|
||||
Cart::add(['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => ['size' => 'large']]);
|
||||
Cart::add(['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'weight' => 550, 'options' => ['size' => 'large']]);
|
||||
```
|
||||
|
||||
New in version 2 of the package is the possibility to work with the [Buyable](#buyable) interface. The way this works is that you have a model implement the [Buyable](#buyable) interface, which will make you implement a few methods so the package knows how to get the id, name and price from your model.
|
||||
@@ -92,8 +80,8 @@ You can just pass the `add()` method an array of arrays, or an array of Buyables
|
||||
|
||||
```php
|
||||
Cart::add([
|
||||
['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00],
|
||||
['id' => '4832k', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00, 'options' => ['size' => 'large']]
|
||||
['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00, 'weight' => 550],
|
||||
['id' => '4832k', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00, 'weight' => 550, 'options' => ['size' => 'large']]
|
||||
]);
|
||||
|
||||
Cart::add([$product1, $product2]);
|
||||
@@ -164,6 +152,24 @@ If you want to completely remove the content of a cart, you can call the destroy
|
||||
Cart::destroy();
|
||||
```
|
||||
|
||||
### Cart::weight()
|
||||
|
||||
The `weight()` method can be used to get the weight total of all items in the cart, given there weight and quantity.
|
||||
|
||||
```php
|
||||
Cart::weight();
|
||||
```
|
||||
|
||||
The method will automatically format the result, which you can tweak using the three optional parameters
|
||||
|
||||
```php
|
||||
Cart::weight($decimals, $decimalSeperator, $thousandSeperator);
|
||||
```
|
||||
|
||||
You can set the default number format in the config file.
|
||||
|
||||
**If you're not using the Facade, but use dependency injection in your (for instance) Controller, you can also simply get the total property `$cart->weight`**
|
||||
|
||||
### Cart::total()
|
||||
|
||||
The `total()` method can be used to get the calculated total of all items in the cart, given there price and quantity.
|
||||
@@ -385,12 +391,12 @@ If you want to switch instances, you just call `Cart::instance('otherInstance')`
|
||||
So a little example:
|
||||
|
||||
```php
|
||||
Cart::instance('shopping')->add('192ao12', 'Product 1', 1, 9.99);
|
||||
Cart::instance('shopping')->add('192ao12', 'Product 1', 1, 9.99, 550);
|
||||
|
||||
// Get the content of the 'shopping' cart
|
||||
Cart::content();
|
||||
|
||||
Cart::instance('wishlist')->add('sdjk922', 'Product 2', 1, 19.95, ['size' => 'medium']);
|
||||
Cart::instance('wishlist')->add('sdjk922', 'Product 2', 1, 19.95, 550, ['size' => 'medium']);
|
||||
|
||||
// Get the content of the 'wishlist' cart
|
||||
Cart::content();
|
||||
@@ -463,7 +469,7 @@ Here is an example:
|
||||
```php
|
||||
|
||||
// First we'll add the item to the cart.
|
||||
$cartItem = Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']);
|
||||
$cartItem = Cart::add('293ad', 'Product 1', 1, 9.99, 550, ['size' => 'large']);
|
||||
|
||||
// Next we associate a model with the item.
|
||||
Cart::associate($cartItem->rowId, 'Product');
|
||||
@@ -472,7 +478,7 @@ Cart::associate($cartItem->rowId, 'Product');
|
||||
$cartItem->associate('Product');
|
||||
|
||||
// You can even make it a one-liner
|
||||
Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large'])->associate('Product');
|
||||
Cart::add('293ad', 'Product 1', 1, 9.99, 550, ['size' => 'large'])->associate('Product');
|
||||
|
||||
// Now, when iterating over the content of the cart, you can access the model.
|
||||
foreach(Cart::content() as $row) {
|
||||
|
||||
177
src/Cart.php
177
src/Cart.php
@@ -110,14 +110,14 @@ class Cart
|
||||
*/
|
||||
public function add($id, $name = null, $qty = null, $price = null, array $options = [])
|
||||
{
|
||||
if ($this->isMulti($id)) {
|
||||
if ($this->isMulti($id))
|
||||
{
|
||||
return array_map(function ($item) {
|
||||
return $this->add($item);
|
||||
}, $id);
|
||||
}
|
||||
|
||||
$cartItem = $this->createCartItem($id, $name, $qty, $price, $options);
|
||||
|
||||
return $this->addCartItem($cartItem);;
|
||||
}
|
||||
|
||||
@@ -139,9 +139,8 @@ class Cart
|
||||
|
||||
$content = $this->getContent();
|
||||
|
||||
if ($content->has($item->rowId)) {
|
||||
if ($content->has($item->rowId))
|
||||
$item->qty += $content->get($item->rowId)->qty;
|
||||
}
|
||||
|
||||
$content->put($item->rowId, $item);
|
||||
|
||||
@@ -163,31 +162,34 @@ class Cart
|
||||
{
|
||||
$cartItem = $this->get($rowId);
|
||||
|
||||
if ($qty instanceof Buyable) {
|
||||
if ($qty instanceof Buyable)
|
||||
$cartItem->updateFromBuyable($qty);
|
||||
} elseif (is_array($qty)) {
|
||||
elseif (is_array($qty))
|
||||
$cartItem->updateFromArray($qty);
|
||||
} else {
|
||||
else
|
||||
$cartItem->qty = $qty;
|
||||
}
|
||||
|
||||
|
||||
$content = $this->getContent();
|
||||
|
||||
if ($rowId !== $cartItem->rowId) {
|
||||
if ($rowId !== $cartItem->rowId)
|
||||
{
|
||||
$content->pull($rowId);
|
||||
|
||||
if ($content->has($cartItem->rowId)) {
|
||||
if ($content->has($cartItem->rowId))
|
||||
{
|
||||
$existingCartItem = $this->get($cartItem->rowId);
|
||||
$cartItem->setQuantity($existingCartItem->qty + $cartItem->qty);
|
||||
}
|
||||
}
|
||||
|
||||
if ($cartItem->qty <= 0) {
|
||||
if ($cartItem->qty <= 0)
|
||||
{
|
||||
$this->remove($cartItem->rowId);
|
||||
return;
|
||||
} else {
|
||||
$content->put($cartItem->rowId, $cartItem);
|
||||
}
|
||||
else
|
||||
$content->put($cartItem->rowId, $cartItem);
|
||||
|
||||
$this->events->fire('cart.updated', $cartItem);
|
||||
|
||||
@@ -248,10 +250,8 @@ class Cart
|
||||
*/
|
||||
public function content()
|
||||
{
|
||||
if (is_null($this->session->get($this->instance))) {
|
||||
if (is_null($this->session->get($this->instance)))
|
||||
return new Collection([]);
|
||||
}
|
||||
|
||||
return $this->session->get($this->instance);
|
||||
}
|
||||
|
||||
@@ -262,9 +262,7 @@ class Cart
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
return $content->sum('qty');
|
||||
return $this->getContent()->sum('qty');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -274,9 +272,7 @@ class Cart
|
||||
*/
|
||||
public function countInstances()
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
return $content->count();
|
||||
return $this->getContent()->count();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -286,13 +282,9 @@ class Cart
|
||||
*/
|
||||
public function totalFloat()
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$total = $content->reduce(function ($total, CartItem $cartItem) {
|
||||
return $this->getContent()->reduce(function ($total, CartItem $cartItem) {
|
||||
return $total + $cartItem->total;
|
||||
}, 0);
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -315,13 +307,9 @@ class Cart
|
||||
*/
|
||||
public function taxFloat()
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$tax = $content->reduce(function ($tax, CartItem $cartItem) {
|
||||
return $this->getContent()->reduce(function ($tax, CartItem $cartItem) {
|
||||
return $tax + $cartItem->taxTotal;
|
||||
}, 0);
|
||||
|
||||
return $tax;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -344,13 +332,9 @@ class Cart
|
||||
*/
|
||||
public function subtotalFloat()
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$subTotal = $content->reduce(function ($subTotal, CartItem $cartItem) {
|
||||
return $this->getContent()->reduce(function ($subTotal, CartItem $cartItem) {
|
||||
return $subTotal + $cartItem->subtotal;
|
||||
}, 0);
|
||||
|
||||
return $subTotal;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -373,13 +357,9 @@ class Cart
|
||||
*/
|
||||
public function discountFloat()
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$discount = $content->reduce(function ($discount, CartItem $cartItem) {
|
||||
return $this->getContent()->reduce(function ($discount, CartItem $cartItem) {
|
||||
return $discount + $cartItem->discountTotal;
|
||||
}, 0);
|
||||
|
||||
return $discount;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -402,13 +382,9 @@ class Cart
|
||||
*/
|
||||
public function initialFloat()
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$initial = $content->reduce(function ($initial, CartItem $cartItem) {
|
||||
return $this->getContent()->reduce(function ($initial, CartItem $cartItem) {
|
||||
return $initial + ($cartItem->qty * $cartItem->price);
|
||||
}, 0);
|
||||
|
||||
return $initial;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -424,6 +400,31 @@ 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()
|
||||
{
|
||||
return $this->getContent()->reduce(function ($total, CartItem $cartItem) {
|
||||
return $total + ($cartItem->qty * $cartItem->weight);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
@@ -432,9 +433,7 @@ class Cart
|
||||
*/
|
||||
public function search(Closure $search)
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
return $content->filter($search);
|
||||
return $this->getContent()->filter($search);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -446,9 +445,8 @@ 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);
|
||||
|
||||
@@ -492,7 +490,8 @@ class Cart
|
||||
$this->taxRate = $taxRate;
|
||||
|
||||
$content = $this->getContent();
|
||||
if ($content && $content->count()) {
|
||||
if ($content && $content->count())
|
||||
{
|
||||
$content->each(function ($item, $key) {
|
||||
$item->setTaxRate($this->taxRate);
|
||||
});
|
||||
@@ -530,7 +529,8 @@ class Cart
|
||||
$this->discount = $discount;
|
||||
|
||||
$content = $this->getContent();
|
||||
if ($content && $content->count()) {
|
||||
if ($content && $content->count())
|
||||
{
|
||||
$content->each(function ($item, $key) {
|
||||
$item->setDiscountRate($this->discount);
|
||||
});
|
||||
@@ -547,9 +547,8 @@ class Cart
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
if ($this->storedCartWithIdentifierExists($identifier)) {
|
||||
if ($this->storedCartWithIdentifierExists($identifier))
|
||||
throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored.");
|
||||
}
|
||||
|
||||
$this->getConnection()->table($this->getTableName())->insert([
|
||||
'identifier' => $identifier,
|
||||
@@ -568,9 +567,8 @@ class Cart
|
||||
*/
|
||||
public function restore($identifier)
|
||||
{
|
||||
if( ! $this->storedCartWithIdentifierExists($identifier)) {
|
||||
if( ! $this->storedCartWithIdentifierExists($identifier))
|
||||
return;
|
||||
}
|
||||
|
||||
$stored = $this->getConnection()->table($this->getTableName())
|
||||
->where('identifier', $identifier)->first();
|
||||
@@ -583,9 +581,8 @@ class Cart
|
||||
|
||||
$content = $this->getContent();
|
||||
|
||||
foreach ($storedContent as $cartItem) {
|
||||
foreach ($storedContent as $cartItem)
|
||||
$content->put($cartItem->rowId, $cartItem);
|
||||
}
|
||||
|
||||
$this->events->fire('cart.restored');
|
||||
|
||||
@@ -603,22 +600,22 @@ 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;
|
||||
}
|
||||
if( ! $this->storedCartWithIdentifierExists($identifier))
|
||||
return false;
|
||||
|
||||
$stored = $this->getConnection()->table($this->getTableName())
|
||||
->where('identifier', $identifier)->first();
|
||||
|
||||
$storedContent = unserialize($stored->content);
|
||||
|
||||
foreach ($storedContent as $cartItem) {
|
||||
foreach ($storedContent as $cartItem)
|
||||
$this->addCartItem($cartItem, $keepDiscount, $keepTax);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -629,20 +626,18 @@ class Cart
|
||||
*/
|
||||
public function __get($attribute)
|
||||
{
|
||||
if($attribute === 'total') {
|
||||
switch($attribute)
|
||||
{
|
||||
case 'total':
|
||||
return $this->total();
|
||||
}
|
||||
|
||||
if($attribute === 'tax') {
|
||||
case 'tax':
|
||||
return $this->tax();
|
||||
}
|
||||
|
||||
if($attribute === 'subtotal') {
|
||||
case 'subtotal':
|
||||
return $this->subtotal();
|
||||
}
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the carts content, if there is no cart content set yet, return a new empty Collection
|
||||
@@ -651,11 +646,9 @@ class Cart
|
||||
*/
|
||||
protected function getContent()
|
||||
{
|
||||
$content = $this->session->has($this->instance)
|
||||
? $this->session->get($this->instance)
|
||||
: new Collection;
|
||||
|
||||
return $content;
|
||||
if ($this->session->has($this->instance))
|
||||
return $this->session->get($this->instance);
|
||||
return new Collection;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -693,8 +686,8 @@ class Cart
|
||||
*/
|
||||
private function isMulti($item)
|
||||
{
|
||||
if ( ! is_array($item)) return false;
|
||||
|
||||
if ( ! is_array($item))
|
||||
return false;
|
||||
return is_array(head($item)) || head($item) instanceof Buyable;
|
||||
}
|
||||
|
||||
@@ -714,9 +707,7 @@ class Cart
|
||||
*/
|
||||
private function getConnection()
|
||||
{
|
||||
$connectionName = $this->getConnectionName();
|
||||
|
||||
return app(DatabaseManager::class)->connection($connectionName);
|
||||
return app(DatabaseManager::class)->connection($this->getConnectionName());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -752,15 +743,13 @@ class Cart
|
||||
*/
|
||||
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
|
||||
{
|
||||
if(is_null($decimals)){
|
||||
$decimals = is_null(config('cart.format.decimals')) ? 2 : config('cart.format.decimals');
|
||||
}
|
||||
if(is_null($decimalPoint)){
|
||||
$decimalPoint = is_null(config('cart.format.decimal_point')) ? '.' : config('cart.format.decimal_point');
|
||||
}
|
||||
if(is_null($thousandSeperator)){
|
||||
$thousandSeperator = is_null(config('cart.format.thousand_separator')) ? ',' : config('cart.format.thousand_separator');
|
||||
}
|
||||
if(is_null($decimals))
|
||||
$decimals = config('cart.format.decimals', 2);
|
||||
if(is_null($decimalPoint))
|
||||
$decimalPoint = config('cart.format.decimal_point', '.');
|
||||
|
||||
if(is_null($thousandSeperator))
|
||||
$thousandSeperator = config('cart.format.thousand_separator', ',');
|
||||
|
||||
return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
@@ -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,10 +101,24 @@ 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted price without TAX.
|
||||
*
|
||||
@@ -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));
|
||||
|
||||
@@ -313,43 +335,35 @@ class CartItem implements Arrayable, Jsonable
|
||||
return $this->{$attribute};
|
||||
}
|
||||
|
||||
if($attribute === 'discount') {
|
||||
switch($attribute)
|
||||
{
|
||||
case 'discount':
|
||||
return $this->price * ($this->discountRate / 100);
|
||||
}
|
||||
|
||||
if($attribute === 'priceTarget') {
|
||||
case 'priceTarget':
|
||||
return $this->price - $this->discount;
|
||||
}
|
||||
|
||||
if($attribute === 'subtotal') {
|
||||
return $this->qty * $this->priceTarget;
|
||||
}
|
||||
|
||||
if($attribute === 'tax') {
|
||||
case 'subtotal':
|
||||
return $this->priceTarget * $this->qty;
|
||||
case 'tax':
|
||||
return $this->priceTarget * ($this->taxRate / 100);
|
||||
}
|
||||
|
||||
if($attribute === 'priceTax') {
|
||||
case 'priceTax':
|
||||
return $this->priceTarget + $this->tax;
|
||||
}
|
||||
|
||||
if($attribute === 'total') {
|
||||
return $this->qty * $this->priceTax;
|
||||
}
|
||||
|
||||
if($attribute === 'taxTotal') {
|
||||
case 'total':
|
||||
return $this->priceTax * $this->qty;
|
||||
case 'taxTotal':
|
||||
return $this->tax * $this->qty;
|
||||
}
|
||||
|
||||
if($attribute === 'discountTotal') {
|
||||
case 'discountTotal':
|
||||
return $this->discount * $this->qty;
|
||||
}
|
||||
case 'weightTotal':
|
||||
return $this->weight * $this->qty;
|
||||
|
||||
if($attribute === 'model' && isset($this->associatedModel)) {
|
||||
case 'model':
|
||||
if (isset($this->associatedModel))
|
||||
return with(new $this->associatedModel)->find($this->id);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -361,7 +375,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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -374,7 +388,7 @@ class CartItem implements Arrayable, Jsonable
|
||||
{
|
||||
$options = array_get($attributes, 'options', []);
|
||||
|
||||
return new self($attributes['id'], $attributes['name'], $attributes['price'], $options);
|
||||
return new self($attributes['id'], $attributes['name'], $attributes['price'], $attributes['weight'], $options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -386,9 +400,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, array $options = [])
|
||||
{
|
||||
return new self($id, $name, $price, $options);
|
||||
return new self($id, $name, $price, $weight, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -418,6 +432,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,
|
||||
@@ -447,17 +462,14 @@ class CartItem implements Arrayable, Jsonable
|
||||
*/
|
||||
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
|
||||
{
|
||||
if (is_null($decimals)){
|
||||
$decimals = is_null(config('cart.format.decimals')) ? 2 : config('cart.format.decimals');
|
||||
}
|
||||
if (is_null($decimals))
|
||||
$decimals = config('cart.format.decimals', 2);
|
||||
|
||||
if (is_null($decimalPoint)){
|
||||
$decimalPoint = is_null(config('cart.format.decimal_point')) ? '.' : config('cart.format.decimal_point');
|
||||
}
|
||||
if (is_null($decimalPoint))
|
||||
$decimalPoint = config('cart.format.decimal_point', '.');
|
||||
|
||||
if (is_null($thousandSeperator)){
|
||||
$thousandSeperator = is_null(config('cart.format.thousand_separator')) ? ',' : config('cart.format.thousand_separator');
|
||||
}
|
||||
if (is_null($thousandSeperator))
|
||||
$thousandSeperator = config('cart.format.thousand_separator', ',');
|
||||
|
||||
return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -22,7 +22,7 @@ class CartItemTest extends TestCase
|
||||
/** @test */
|
||||
public function it_can_be_cast_to_an_array()
|
||||
{
|
||||
$cartItem = new CartItem(1, 'Some item', 10.00, ['size' => 'XL', 'color' => 'red']);
|
||||
$cartItem = new CartItem(1, 'Some item', 10.00, 550, ['size' => 'XL', 'color' => 'red']);
|
||||
$cartItem->setQuantity(2);
|
||||
|
||||
$this->assertEquals([
|
||||
@@ -37,19 +37,20 @@ class CartItemTest extends TestCase
|
||||
],
|
||||
'tax' => 0,
|
||||
'subtotal' => 20.00,
|
||||
'discount' => 0.0
|
||||
'discount' => 0.0,
|
||||
'weight' => 550.0
|
||||
], $cartItem->toArray());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_be_cast_to_json()
|
||||
{
|
||||
$cartItem = new CartItem(1, 'Some item', 10.00, ['size' => 'XL', 'color' => 'red']);
|
||||
$cartItem = new CartItem(1, 'Some item', 10.00, 550, ['size' => 'XL', 'color' => 'red']);
|
||||
$cartItem->setQuantity(2);
|
||||
|
||||
$this->assertJson($cartItem->toJson());
|
||||
|
||||
$json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","qty":2,"price":10,"options":{"size":"XL","color":"red"},"discount":0,"tax":0,"subtotal":20}';
|
||||
$json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","qty":2,"price":10,"weight":550,"options":{"size":"XL","color":"red"},"discount":0,"tax":0,"subtotal":20}';
|
||||
|
||||
$this->assertEquals($json, $cartItem->toJson());
|
||||
}
|
||||
|
||||
@@ -166,7 +166,7 @@ class CartTest extends TestCase
|
||||
|
||||
$cart = $this->getCart();
|
||||
|
||||
$cart->add(['id' => 1, 'name' => 'Test item', 'qty' => 1, 'price' => 10.00]);
|
||||
$cart->add(['id' => 1, 'name' => 'Test item', 'qty' => 1, 'price' => 10.00, 'weight' => 550]);
|
||||
|
||||
$this->assertEquals(1, $cart->count());
|
||||
|
||||
@@ -181,8 +181,8 @@ class CartTest extends TestCase
|
||||
$cart = $this->getCart();
|
||||
|
||||
$cart->add([
|
||||
['id' => 1, 'name' => 'Test item 1', 'qty' => 1, 'price' => 10.00],
|
||||
['id' => 2, 'name' => 'Test item 2', 'qty' => 1, 'price' => 10.00]
|
||||
['id' => 1, 'name' => 'Test item 1', 'qty' => 1, 'price' => 10.00, 'weight' => 550],
|
||||
['id' => 2, 'name' => 'Test item 2', 'qty' => 1, 'price' => 10.00, 'weight' => 550]
|
||||
]);
|
||||
|
||||
$this->assertEquals(2, $cart->count());
|
||||
@@ -488,7 +488,8 @@ class CartTest extends TestCase
|
||||
'tax' => 2.10,
|
||||
'subtotal' => 10.0,
|
||||
'options' => [],
|
||||
'discount' => 0.0
|
||||
'discount' => 0.0,
|
||||
'weight' => 0.0
|
||||
],
|
||||
'370d08585360f5c568b18d1f2e4ca1df' => [
|
||||
'rowId' => '370d08585360f5c568b18d1f2e4ca1df',
|
||||
@@ -499,7 +500,8 @@ class CartTest extends TestCase
|
||||
'tax' => 2.10,
|
||||
'subtotal' => 10.0,
|
||||
'options' => [],
|
||||
'discount' => 0.0
|
||||
'discount' => 0.0,
|
||||
'weight' => 0.0
|
||||
]
|
||||
], $content->toArray());
|
||||
}
|
||||
@@ -997,6 +999,75 @@ class CartTest extends TestCase
|
||||
$this->assertEquals(10, $cart3->totalFloat());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_cant_merge_non_existing_cart()
|
||||
{
|
||||
$this->artisan('migrate', [
|
||||
'--database' => 'testing',
|
||||
]);
|
||||
Event::fake();
|
||||
$cart = $this->getCartDiscount(50);
|
||||
$cart->add( new BuyableProduct(1, 'Item', 10.00), 1);
|
||||
$cart->add( new BuyableProduct(2, 'Item 2', 10.00), 1);
|
||||
$this->assertEquals(false, $cart->merge('doesNotExist'));
|
||||
$this->assertEquals(2, $cart->countInstances());
|
||||
}
|
||||
/** @test */
|
||||
public function cart_can_calculate_all_values()
|
||||
{
|
||||
$cart = $this->getCartDiscount( 50 );
|
||||
$cart->add(new BuyableProduct(1, 'First item', 10.00), 1);
|
||||
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
|
||||
$cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19);
|
||||
$this->assertEquals('10.00', $cart->initial(2));
|
||||
$this->assertEquals(10.00, $cart->initialFloat());
|
||||
$this->assertEquals('5.00', $cart->discount(2));
|
||||
$this->assertEquals(5.00, $cart->discountFloat());
|
||||
$this->assertEquals('5.00', $cart->subtotal(2));
|
||||
$this->assertEquals(5.00, $cart->subtotalFloat());
|
||||
$this->assertEquals('0.95', $cart->tax(2));
|
||||
$this->assertEquals(0.95, $cart->taxFloat());
|
||||
$this->assertEquals('5.95', $cart->total(2));
|
||||
$this->assertEquals(5.95, $cart->totalFloat());
|
||||
}
|
||||
/** @test */
|
||||
public function can_access_cart_item_propertys()
|
||||
{
|
||||
$cart = $this->getCartDiscount( 50 );
|
||||
$cart->add(new BuyableProduct(1, 'First item', 10.00), 1);
|
||||
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
|
||||
$this->assertEquals(50, $cartItem->discountRate);
|
||||
}
|
||||
/** @test */
|
||||
public function cant_access_non_existant_propertys()
|
||||
{
|
||||
$cart = $this->getCartDiscount( 50 );
|
||||
$cart->add(new BuyableProduct(1, 'First item', 10.00), 1);
|
||||
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
|
||||
$this->assertEquals(null, $cartItem->doesNotExist);
|
||||
$this->assertEquals(null, $cart->doesNotExist);
|
||||
}
|
||||
/** @test */
|
||||
public function can_set_cart_item_discount()
|
||||
{
|
||||
$cart = $this->getCart();
|
||||
$cart->add(new BuyableProduct(1, 'First item', 10.00), 1);
|
||||
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
|
||||
$cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 50);
|
||||
$this->assertEquals(50, $cartItem->discountRate);
|
||||
}
|
||||
/** @test */
|
||||
public function can_set_cart_item_weight_and_calculate_total_weight()
|
||||
{
|
||||
$cart = $this->getCart();
|
||||
$cart->add(new BuyableProduct(1, 'First item', 10.00, 250), 2);
|
||||
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
|
||||
$cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 50);
|
||||
$this->assertEquals('500.00', $cart->weight(2));
|
||||
$this->assertEquals(500.00, $cart->weightFloat());
|
||||
$this->assertEquals(500.00, $cartItem->weightTotal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an instance of the cart.
|
||||
*
|
||||
|
||||
@@ -21,6 +21,11 @@ class BuyableProduct implements Buyable
|
||||
*/
|
||||
private $price;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $weight;
|
||||
|
||||
/**
|
||||
* BuyableProduct constructor.
|
||||
*
|
||||
@@ -28,11 +33,12 @@ class BuyableProduct implements Buyable
|
||||
* @param string $name
|
||||
* @param float $price
|
||||
*/
|
||||
public function __construct($id = 1, $name = 'Item name', $price = 10.00)
|
||||
public function __construct($id = 1, $name = 'Item name', $price = 10.00, $weight = 0)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->price = $price;
|
||||
$this->weight = $weight;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,4 +70,14 @@ class BuyableProduct implements Buyable
|
||||
{
|
||||
return $this->price;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the price of the Buyable item.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getBuyableWeight($options = null)
|
||||
{
|
||||
return $this->weight;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user