Skip to content

Replaced array_get() with Arr::get(), array_get() removed in Laravel 6.0 #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 69 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
53eaa89
fix links in Readme
raakesh Feb 27, 2018
71abbd2
Merge pull request #1 from raakesh/raakesh-fix-links-readme
raakesh Feb 27, 2018
3a4adbd
Add Buyable interface path to documentation
awoyele Feb 27, 2018
3e4f5a1
Fixes tax rounding issue
olimortimer Mar 13, 2018
5a75e93
added section about Buyable interface to README
KristobalJunta Apr 20, 2018
c8f5d40
Update composer.json
hardevine Sep 19, 2018
673bc57
Update composer.json
hardevine Sep 19, 2018
d1388a8
Update README.md
hardevine Oct 28, 2018
861de3d
Update CartItem.php
hardevine Nov 13, 2018
926b4c5
Merge pull request #1 from KristobalJunta/master
hardevine Nov 26, 2018
01473f1
Merge pull request #3 from raakesh/master
hardevine Nov 26, 2018
965f141
Merge pull request #2 from awoyele/master
hardevine Nov 26, 2018
2a6e62e
Update Cart.php
hardevine Nov 26, 2018
2c9c27d
Update CartTest.php
hardevine Nov 26, 2018
67c9364
Update README.md
hardevine Nov 26, 2018
8115c4e
Update CartItemTest.php
hardevine Nov 26, 2018
09ab68d
Update CartTest.php
hardevine Nov 26, 2018
927f0c1
Update README.md
hardevine Nov 26, 2018
22bb9ca
Update README.md
hardevine Nov 26, 2018
a25541e
Update Cart.php
hardevine Dec 8, 2018
63250f3
Update Cart.php
hardevine Dec 15, 2018
948616b
Update CartTest.php
hardevine Dec 15, 2018
b8fe654
Update Cart.php
hardevine Dec 31, 2018
172a6c5
Merge pull request #5 from olimortimer/master
hardevine Jan 31, 2019
f4e6408
Update CartItemTest.php
hardevine Jan 31, 2019
9f2222f
Update CartItemTest.php
hardevine Jan 31, 2019
daeec55
Update CartItem.php
hardevine Jan 31, 2019
4573962
Update composer.json
hardevine Mar 5, 2019
076c1d2
Update event->fire to event->dispatch
drehimself Mar 17, 2019
05de8fd
Merge pull request #6 from drehimself/master
hardevine Mar 18, 2019
d9185d0
Update Cart.php
hardevine Apr 20, 2019
4fbeb25
Update 0000_00_00_000000_create_shoppingcart_table.php
hardevine Apr 20, 2019
f598dbf
Update Cart.php
hardevine Apr 20, 2019
4855d38
Update 0000_00_00_000000_create_shoppingcart_table.php
hardevine Apr 20, 2019
7571d4e
Update .travis.yml
hardevine Apr 20, 2019
599fb6d
Update Cart.php
hardevine Apr 20, 2019
0ef89eb
Update composer.json
hardevine Sep 12, 2019
4ea0f32
Update composer.json
hardevine Nov 28, 2019
5fc2566
Update CartItem.php
szabizs Jan 3, 2020
9aad9e3
Merge pull request #7 from szabizs/patch-1
hardevine Jan 13, 2020
05ddc67
Database query also check for instance
Feb 15, 2020
804c759
Merge pull request #8 from Itxshakiil/master
hardevine Feb 19, 2020
46b3ab6
add items with different taxrates
lkrempler Feb 21, 2020
3244936
Added multiple taxrates to readme
lkrempler Feb 21, 2020
14ddf6b
Support Laravel 7
divdax Mar 4, 2020
0e93e13
Merge pull request #11 from divdax/patch-1
hardevine Mar 5, 2020
c52cec1
update composer.json
owiesnama Mar 9, 2020
2da55ec
Merge branch 'master' into support-laravel-7.0.8
owiesnama Mar 9, 2020
4510098
toJson return with model if available
Mar 19, 2020
ad9b663
Merge pull request #12 from owiesnama/support-laravel-7.0.8
hardevine Mar 19, 2020
3092880
Merge pull request #9 from lkrempler/newTaxes
hardevine Mar 19, 2020
96cbee2
Update composer.json
winkelco Mar 25, 2020
1dcc4ae
Merge pull request #14 from winkelco/patch-1
hardevine Apr 15, 2020
fe4f4c0
Merge pull request #13 from Itxshakiil/patch-1
hardevine Apr 15, 2020
00e180b
Update CartItem.php
hardevine Apr 15, 2020
fbb271b
Update Cart.php
OscarKolsrud Jul 4, 2020
08f7e19
Merge pull request #15 from OscarKolsrud/patch-1
hardevine Jul 5, 2020
2fc6eff
support v8
hardevine Sep 6, 2020
e72f59b
Update README.md
hardevine Mar 28, 2021
f3acf54
Update Cart.php
hardevine May 19, 2021
8dea942
Update Cart.php
hardevine May 26, 2021
990c872
add support to Laravel 9
manelds Feb 16, 2022
fc73491
Merge pull request #45 from manelds/master
hardevine Feb 17, 2022
df52560
Update composer.json
hardevine Mar 9, 2023
4290204
Update README.md
hardevine Sep 21, 2023
33b12f4
added support for laravel version 11
arif98741 Mar 30, 2024
0785c08
Merge pull request #57 from PHPDark/master
hardevine Mar 30, 2024
1f3fc08
added support for laravel version 12
gaetandezeiraud Mar 17, 2025
023025c
Merge pull request #60 from gaetandezeiraud/master
hardevine Mar 17, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
language: php

php:
- 5.6
- 7.0

before_script:
- composer self-update
- composer install --prefer-source --no-interaction

script: vendor/bin/phpunit
script: vendor/bin/phpunit
103 changes: 79 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## 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)
[![Build Status](https://travis-ci.org/hardevine/LaravelShoppingcart.png?branch=master)](https://travis-ci.org/hardevine/LaravelShoppingcart)
[![Total Downloads](https://poser.pugx.org/hardevine/shoppingcart/downloads.png)](https://packagist.org/packages/hardevine/shoppingcart)
[![Latest Stable Version](https://poser.pugx.org/hardevine/shoppingcart/v/stable)](https://packagist.org/packages/hardevine/shoppingcart)
[![Latest Unstable Version](https://poser.pugx.org/hardevine/shoppingcart/v/unstable)](https://packagist.org/packages/hardevine/shoppingcart)
[![License](https://poser.pugx.org/gloudemans/shoppingcart/license)](https://packagist.org/packages/gloudemans/shoppingcart)

A simple shoppingcart implementation for Laravel.
Expand All @@ -12,21 +12,21 @@ A simple shoppingcart implementation for Laravel.
Install the package through [Composer](http://getcomposer.org/).

Run the Composer require command from the Terminal:

composer require gloudemans/shoppingcart

If you're using Laravel 5.5, this is all there is to do.
```bash
composer require hardevine/shoppingcart
```
If you're using Laravel 5.5 or above, 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

```php
Gloudemans\Shoppingcart\ShoppingcartServiceProvider::class
```
And optionally add a new line to the `aliases` array:

'Cart' => Gloudemans\Shoppingcart\Facades\Cart::class,

```php
'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**
Expand Down Expand Up @@ -63,6 +63,11 @@ As an optional fifth parameter you can pass it options, so you can add multiple
Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']);
```

You can optional pass different taxrates to the items.
```php
Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large'], 20);
```

**The `add()` method will return an CartItem instance of the item you just added to the cart.**

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.
Expand All @@ -71,9 +76,13 @@ Maybe you prefer to add the item using an array? As long as the array contains t
Cart::add(['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => ['size' => 'large']]);
```

New in version 2 of the package is the possibility to work with the `Buyable` interface. The way this works is that you have a model implement the `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.
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` interface, which will make you implement a few methods so the package knows how to get the id, name and price from your model.
This way you can just pass the `add()` method a model and the quantity and it will automatically add it to the cart.

The path to the `Buyable` interface is:
```php
Gloudemans\Shoppingcart\Contracts\Buyable;
```
**As an added bonus it will automatically associate the model with the CartItem**

```php
Expand Down Expand Up @@ -325,43 +334,89 @@ foreach(Cart::content() as $row) {
echo 'You have ' . $row->qty . ' items of ' . $row->model->name . ' with description: "' . $row->model->description . '" in your cart.';
}
```

### Buyable

For the convenience of faster adding items to cart and their automatic association, your model can implement `Buyable` interface. To do so, it must implement such functions:

```php
public function getBuyableIdentifier(){
return $this->id;
}

public function getBuyableDescription(){
return $this->name;
}

public function getBuyablePrice(){
return $this->price;
}
```

Example:

```php
<?php

namespace App\Models;

use Gloudemans\Shoppingcart\Contracts\Buyable;
use Illuminate\Database\Eloquent\Model;

class Product exends Model implements Buyable {
public function getBuyableIdentifier($options = null) {
return $this->id;
}

public function getBuyableDescription($options = null) {
return $this->name;
}

public function getBuyablePrice($options = null) {
return $this->price;
}
}
```


## Database

- [Config](#configuration)
- [Storing the cart](#save-cart-to-database)
- [Restoring the cart](#retrieve-cart-from-database)
- [Storing the cart](#storing-the-cart)
- [Restoring the cart](#restoring-the-cart)

### Configuration
To save cart into the database so you can retrieve it later, the package needs to know which database connection to use and what the name of the table is.
By default the package will use the default database connection and use a table named `shoppingcart`.
If you want to change these options, you'll have to publish the `config` file.

```bash
php artisan vendor:publish --provider="Gloudemans\Shoppingcart\ShoppingcartServiceProvider" --tag="config"

```
This will give you a `cart.php` config file in which you can make the changes.

To make your life easy, the package also includes a ready to use `migration` which you can publish by running:

```bash
php artisan vendor:publish --provider="Gloudemans\Shoppingcart\ShoppingcartServiceProvider" --tag="migrations"
```
This will place a `shoppingcart` table's migration file into `database/migrations` directory. Now all you have to do is run `php artisan migrate` to migrate your database.

### Storing the cart
To store your cart instance into the database, you have to call the `store($identifier) ` method. Where `$identifier` is a random key, for instance the id or username of the user.

```php
Cart::store('username');

// To store a cart instance named 'wishlist'
Cart::instance('wishlist')->store('username');

```
### Restoring the cart
If you want to retrieve the cart from the database and restore it, all you have to do is call the `restore($identifier)` where `$identifier` is the key you specified for the `store` method.

```php
Cart::restore('username');

// To restore a cart instance named 'wishlist'
Cart::instance('wishlist')->restore('username');

```
## Exceptions

The Cart package will throw exceptions if something goes wrong. This way it's easier to debug your code using the Cart package or to handle the error based on the type of exceptions. The Cart packages can throw the following exceptions:
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "gloudemans/shoppingcart",
"name": "hardevine/shoppingcart",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont forget this

"description": "Laravel Shoppingcart",
"keywords": ["laravel", "shoppingcart"],
"license": "MIT",
Expand All @@ -10,12 +10,12 @@
}
],
"require": {
"illuminate/support": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.*|| 5.6.* || 5.7.*",
"illuminate/session": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.*|| 5.6.* || 5.7.*",
"illuminate/events": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.*|| 5.6.* || 5.7.*"
"illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6|^7|^8|^9|^10|^11|^12",
"illuminate/session": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6|^7|^8|^9|^10|^11|^12",
"illuminate/events": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6|^7|^8|^9|^10|^11|^12"
},
"require-dev": {
"phpunit/phpunit": "~5.0 || ~6.0 || ~7.0",
"phpunit/phpunit": "~5.0 || ~6.0 || ~7.0 || ~10.0",
"mockery/mockery": "~0.9.0",
"orchestra/testbench": "~3.1"
},
Expand Down
Loading