From 892c76cc619b586125b92288c364ece5bf209182 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Fri, 21 Dec 2018 18:37:36 +0100 Subject: [PATCH] Updated readme minor modifications --- .travis.yml | 1 + README.md | 129 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/Cart.php | 26 ++++++++++- 3 files changed, 148 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 45a6c0e..2eacae4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: php php: - 5.6 - 7.0 + - 7.2 before_script: - composer self-update diff --git a/README.md b/README.md index e4748db..deebafe 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ ## LaravelShoppingcart -[![Build Status](https://travis-ci.org/Crinsane/LaravelShoppingcart.png?branch=master)](https://travis-ci.org/Crinsane/LaravelShoppingcart) -[![Total Downloads](https://poser.pugx.org/gloudemans/shoppingcart/downloads.png)](https://packagist.org/packages/gloudemans/shoppingcart) -[![Latest Stable Version](https://poser.pugx.org/gloudemans/shoppingcart/v/stable)](https://packagist.org/packages/gloudemans/shoppingcart) -[![Latest Unstable Version](https://poser.pugx.org/gloudemans/shoppingcart/v/unstable)](https://packagist.org/packages/gloudemans/shoppingcart) -[![License](https://poser.pugx.org/gloudemans/shoppingcart/license)](https://packagist.org/packages/gloudemans/shoppingcart) +[![Build Status](https://travis-ci.org/bumbummen99/LaravelShoppingcart.png?branch=master)](https://travis-ci.org/bumbummen99/LaravelShoppingcart) +[![Total Downloads](https://poser.pugx.org/gloudemans/shoppingcart/downloads.png)](https://packagist.org/packages/bumbummen99/shoppingcart) +[![Latest Stable Version](https://poser.pugx.org/gloudemans/shoppingcart/v/stable)](https://packagist.org/packages/bumbummen99/shoppingcart) +[![Latest Unstable Version](https://poser.pugx.org/gloudemans/shoppingcart/v/unstable)](https://packagist.org/packages/bumbummen99/shoppingcart) +[![License](https://poser.pugx.org/gloudemans/shoppingcart/license)](https://packagist.org/packages/bumbummen99/shoppingcart) -A simple shoppingcart implementation for Laravel by gloudemans, updated to work with laravel 5.7. This repository and composer package will be removed when gloudemans decides to update the original repository. +This is a fork of [Crisane's LaravelShoppingcart](https://github.com/Crinsane/LaravelShoppingcart) extended with minor features compatible with Laravel 5.7. ## Installation @@ -13,7 +13,7 @@ Install the package through [Composer](http://getcomposer.org/). Run the Composer require command from the Terminal: - composer require gloudemans/shoppingcart + composer require bumbummen99/shoppingcart If you're using Laravel 5.5, this is all there is to do. @@ -217,12 +217,49 @@ 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 subtotal property `$cart->subtotal`** +### Cart::discount() + +The `discount()` method can be used to get the total discount of all items in the cart. + +```php +Cart::discount(); +``` + +The method will automatically format the result, which you can tweak using the three optional parameters + +```php +Cart::discount($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 subtotal property `$cart->discount`** + +### Cart::initial() + +The `initial()` method can be used to get the total price of all items in the cart before discount. + +```php +Cart::initial(); +``` + +The method will automatically format the result, which you can tweak using the three optional parameters + +```php +Cart::initial($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 subtotal property `$cart->initial`** + ### Cart::count() If you want to know how many items there are in your cart, you can use the `count()` method. This method will return the total number of items in the cart. So if you've added 2 books and 1 shirt, it will return 3 items. ```php Cart::count(); +$cart->count(); ``` ### Cart::search() @@ -247,6 +284,42 @@ As you can see the Closure will receive two parameters. The first is the CartIte This way of searching gives you total control over the search process and gives you the ability to create very precise and specific searches. +### Cart::setTax($rowId, $taxRate) + +You can use the `setTax()` method to change the tax rate that applies to the CartItem. This will overwrite the value set in the config file. + +```php +Cart::setTax($rowId, 21); +$cart->setTax($rowId, 21); +``` + +### Cart::setGlobalTax($taxRate) + +You can use the `setGlobalTax()` method to change the tax rate for all items in the cart. New items will receive the setGlobalTax as well. + +```php +Cart::setGlobalDiscount(21); +$cart->setGlobalDiscount(21); +``` + +### Cart::setGlobalDiscount($discountRate) + +You can use the `setGlobalDiscount()` method to change the discount rate for all items in the cart. New items will receive the discount as well. + +```php +Cart::setGlobalDiscount(21); +$cart->setGlobalDiscount(21); +``` + +### Cart::setDiscount($rowId, $taxRate) + +You can use the `setDiscount()` method to change the discount rate that applies a CartItem. Keep in mind that this value will be changed if you set the global discount for the Cart afterwards. + +```php +Cart::setDiscount($rowId, 21); +$cart->setDiscount($rowId, 21); +``` + ## Collections On multiple instances the Cart will return to you a Collection. This is just a simple Laravel Collection, so all methods you can call on a Laravel Collection are also available on the result. @@ -288,6 +361,48 @@ Cart::instance('shopping')->content(); // And the count of the 'wishlist' cart again Cart::instance('wishlist')->count(); +``` + +You can also use the `InstanceIdentifier` Contract to extend a desired Model to assign / create a Cart instance for it. This also allows to directly set the global discount. +``` +email; + } + + /** + * Get the unique identifier to load the Cart from + * + * @return int|string + */ + public function getInstanceGlobalDiscount($options = null) + { + return $this->discountRate ?: 0; + } +} + +// Inside Controller +$user = \Auth::user(); +$cart = Cart::instance($user); + + + ``` **N.B. Keep in mind that the cart stays in the last set instance for as long as you don't set a different one during script execution.** diff --git a/src/Cart.php b/src/Cart.php index 7b58abe..7ba9707 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -45,6 +45,13 @@ class Cart */ private $discount = 0; + /** + * Defines the discount percentage. + * + * @var float + */ + private $taxRate = 0; + /** * Cart constructor. * @@ -55,6 +62,7 @@ class Cart { $this->session = $session; $this->events = $events; + $this->taxRate = config('cart.tax'); $this->instance(self::DEFAULT_INSTANCE); } @@ -392,6 +400,22 @@ class Cart $this->session->put($this->instance, $content); } + /** + * Set the global tax rate for the cart. + * This will set the tax rate for all items. + * + * @param float $discount + */ + public function setGlobalTax(float $taxRate) + { + $this->taxRate = $taxRate; + if ($this->content && $this->content->count()) { + $this->content->each(function ($item, $key) { + $item->setTaxRate($this->taxRate); + }); + } + } + /** * Set the discount rate for the cart item with the given rowId. * @@ -549,7 +573,7 @@ class Cart $cartItem->setQuantity($qty); } - $cartItem->setTaxRate(config('cart.tax')); + $cartItem->setTaxRate($this->taxRate); $cartItem->setDiscountRate( $this->discount ); return $cartItem;