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 37 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
65 changes: 57 additions & 8 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 @@ -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 hardevine/shoppingcart

If you're using Laravel 5.5, this is all there is to do.

Expand Down Expand Up @@ -71,9 +71,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:

Gloudemans\Shoppingcart\Contracts\Buyable;

**As an added bonus it will automatically associate the model with the CartItem**

```php
Expand Down Expand Up @@ -325,11 +329,56 @@ 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.
Expand Down
8 changes: 4 additions & 4 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,9 +10,9 @@
}
],
"require": {
"illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*",
"illuminate/session": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*",
"illuminate/events": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*"
"illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|~6.0",
"illuminate/session": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|~6.0",
"illuminate/events": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|~6.0"
},
"require-dev": {
"phpunit/phpunit": "~5.0|~6.0|~7.0",
Expand Down
58 changes: 40 additions & 18 deletions src/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class Cart
*
* @var \Illuminate\Session\SessionManager
*/
private $session;
protected $session;

/**
* Instance of the event dispatcher.
*
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
private $events;
Expand Down Expand Up @@ -94,7 +94,11 @@ public function add($id, $name = null, $qty = null, $price = null, array $option
}, $id);
}

$cartItem = $this->createCartItem($id, $name, $qty, $price, $options);
if ($id instanceof CartItem) {
$cartItem = $id;
} else {
$cartItem = $this->createCartItem($id, $name, $qty, $price, $options);
}

$content = $this->getContent();

Expand All @@ -103,8 +107,8 @@ public function add($id, $name = null, $qty = null, $price = null, array $option
}

$content->put($cartItem->rowId, $cartItem);
$this->events->fire('cart.added', $cartItem);

$this->events->dispatch('cart.added', $cartItem);

$this->session->put($this->instance, $content);

Expand Down Expand Up @@ -148,7 +152,7 @@ public function update($rowId, $qty)
$content->put($cartItem->rowId, $cartItem);
}

$this->events->fire('cart.updated', $cartItem);
$this->events->dispatch('cart.updated', $cartItem);

$this->session->put($this->instance, $content);

Expand All @@ -169,7 +173,7 @@ public function remove($rowId)

$content->pull($cartItem->rowId);

$this->events->fire('cart.removed', $cartItem);
$this->events->dispatch('cart.removed', $cartItem);

$this->session->put($this->instance, $content);
}
Expand Down Expand Up @@ -350,17 +354,21 @@ public function store($identifier)
{
$content = $this->getContent();

if ($this->storedCartWithIdentifierExists($identifier)) {
throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored.");
}

$this->getConnection()
->table($this->getTableName())
->where('identifier', $identifier)
->delete();


$this->getConnection()->table($this->getTableName())->insert([
'identifier' => $identifier,
'instance' => $this->currentInstance(),
'content' => serialize($content)
'content' => serialize($content),
'created_at'=> new \DateTime()
]);

$this->events->fire('cart.stored');
$this->events->dispatch('cart.stored');
}

/**
Expand Down Expand Up @@ -390,16 +398,30 @@ public function restore($identifier)
$content->put($cartItem->rowId, $cartItem);
}

$this->events->fire('cart.restored');
$this->events->dispatch('cart.restored');

$this->session->put($this->instance, $content);

$this->instance($currentInstance);

$this->getConnection()->table($this->getTableName())
->where('identifier', $identifier)->delete();
}



/**
* Deletes the stored cart with given identifier
*
* @param mixed $identifier
*/
protected function deleteStoredCart($identifier) {
$this->getConnection()
->table($this->getTableName())
->where('identifier', $identifier)
->delete();
}



/**
* Magic method to make accessing the total, tax and subtotal properties possible.
*
Expand Down Expand Up @@ -483,7 +505,7 @@ private function isMulti($item)
* @param $identifier
* @return bool
*/
private function storedCartWithIdentifierExists($identifier)
protected function storedCartWithIdentifierExists($identifier)
{
return $this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->exists();
}
Expand All @@ -493,7 +515,7 @@ private function storedCartWithIdentifierExists($identifier)
*
* @return \Illuminate\Database\Connection
*/
private function getConnection()
protected function getConnection()
{
$connectionName = $this->getConnectionName();

Expand All @@ -505,7 +527,7 @@ private function getConnection()
*
* @return string
*/
private function getTableName()
protected function getTableName()
{
return config('cart.database.table', 'shoppingcart');
}
Expand Down
49 changes: 35 additions & 14 deletions src/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ class CartItem implements Arrayable, Jsonable
*/
private $taxRate = 0;

/**
* Is item saved for later.
*
* @var boolean
*/
private $isSaved = false;

/**
* CartItem constructor.
*
Expand Down Expand Up @@ -242,6 +249,19 @@ public function setTaxRate($taxRate)
return $this;
}

/**
* Set saved state.
*
* @param bool $bool
* @return \Gloudemans\Shoppingcart\CartItem
*/
public function setSaved($bool)
{
$this->isSaved = $bool;

return $this;
}

/**
* Get an attribute from the cart item or get the associated model.
*
Expand All @@ -254,24 +274,24 @@ public function __get($attribute)
return $this->{$attribute};
}

if($attribute === 'priceTax') {
return $this->price + $this->tax;
if ($attribute === 'priceTax') {
return number_format(($this->price + $this->tax), 2, '.', '');
}
if($attribute === 'subtotal') {
return $this->qty * $this->price;

if ($attribute === 'subtotal') {
return number_format(($this->qty * $this->price), 2, '.', '');
}
if($attribute === 'total') {
return $this->qty * ($this->priceTax);

if ($attribute === 'total') {
return number_format(($this->qty * $this->priceTax), 2, '.', '');
}

if($attribute === 'tax') {
return $this->price * ($this->taxRate / 100);
if ($attribute === 'tax') {
return number_format(($this->price * ($this->taxRate / 100)), 2, '.', '');
}
if($attribute === 'taxTotal') {
return $this->tax * $this->qty;

if ($attribute === 'taxTotal') {
return number_format(($this->tax * $this->qty), 2, '.', '');
}

if($attribute === 'model' && isset($this->associatedModel)) {
Expand Down Expand Up @@ -349,6 +369,7 @@ public function toArray()
'price' => $this->price,
'options' => $this->options->toArray(),
'tax' => $this->tax,
'isSaved' => $this->isSaved,
'subtotal' => $this->subtotal
];
}
Expand All @@ -373,7 +394,7 @@ public function toJson($options = 0)
* @param string $thousandSeperator
* @return string
*/
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
private function numberFormat($value, $decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
if (is_null($decimals)){
$decimals = is_null(config('cart.format.decimals')) ? 2 : config('cart.format.decimals');
Expand Down
7 changes: 4 additions & 3 deletions tests/CartItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public function it_can_be_cast_to_an_array()
'size' => 'XL',
'color' => 'red'
],
'tax' => 0,
'tax' => 0.0,
'subtotal' => 20.00,
'isSaved' => false
], $cartItem->toArray());
}

Expand All @@ -48,8 +49,8 @@ public function it_can_be_cast_to_json()

$this->assertJson($cartItem->toJson());

$json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","qty":2,"price":10,"options":{"size":"XL","color":"red"},"tax":0,"subtotal":20}';
$json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","qty":2,"price":10,"options":{"size":"XL","color":"red"},"tax":"0.00","isSaved":false,"subtotal":"20.00"}';

$this->assertEquals($json, $cartItem->toJson());
}
}
}
Loading