PHPackages                             tudorstoicalynx/shoppingcart - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. tudorstoicalynx/shoppingcart

ActiveLibrary[Framework](/categories/framework)

tudorstoicalynx/shoppingcart
============================

Laravel Shoppingcart

2.8.1(7y ago)015MITPHP

Since Mar 14Pushed 6y agoCompare

[ Source](https://github.com/tudorstoicalynx/LaravelShoppingcart)[ Packagist](https://packagist.org/packages/tudorstoicalynx/shoppingcart)[ RSS](/packages/tudorstoicalynx-shoppingcart/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (6)Versions (38)Used By (0)

LaravelShoppingcart
-------------------

[](#laravelshoppingcart)

[![Build Status](https://camo.githubusercontent.com/9b729eb05d5e74d53a6b3b332952991f8dd1172118c929a060ad24405394f0cb/68747470733a2f2f7472617669732d63692e6f72672f62756d62756d6d656e39392f4c61726176656c53686f7070696e67636172742e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/bumbummen99/LaravelShoppingcart)[![codecov](https://camo.githubusercontent.com/37ce74419caaec9cc31fd60eccc70972e8ea412be3ba914d3fcaede32b5e25aa/68747470733a2f2f636f6465636f762e696f2f67682f62756d62756d6d656e39392f4c61726176656c53686f7070696e67636172742f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/bumbummen99/LaravelShoppingcart)[![StyleCI](https://camo.githubusercontent.com/8d74dda29fa86b19c5242138cd03558b6ca8f7ef7089d8a0032058eb59fb5838/68747470733a2f2f7374796c6563692e696f2f7265706f732f3135323631303837382f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/152610878)[![Total Downloads](https://camo.githubusercontent.com/4735b941ce9cd0b851dd1ce536df5c6aa5f7ead993778d363afb562e844d3233/68747470733a2f2f706f7365722e707567782e6f72672f62756d62756d6d656e39392f73686f7070696e67636172742f646f776e6c6f6164732e706e67)](https://packagist.org/packages/bumbummen99/shoppingcart)[![Latest Stable Version](https://camo.githubusercontent.com/1d49894e715fca850e5adc3a807776ad693f10c3aa08b0179dd8eeba3690af7c/68747470733a2f2f706f7365722e707567782e6f72672f62756d62756d6d656e39392f73686f7070696e67636172742f762f737461626c65)](https://packagist.org/packages/bumbummen99/shoppingcart)[![Latest Unstable Version](https://camo.githubusercontent.com/932ddd40fbf6d15fd0c004b292f8b93259cc629d57d6f79e34c024021fcad16f/68747470733a2f2f706f7365722e707567782e6f72672f62756d62756d6d656e39392f73686f7070696e67636172742f762f756e737461626c65)](https://packagist.org/packages/bumbummen99/shoppingcart)[![License](https://camo.githubusercontent.com/67e436af715dd99bbb4f21cfad566c16f8a7614aff29a0a84584a2463b042113/68747470733a2f2f706f7365722e707567782e6f72672f62756d62756d6d656e39392f73686f7070696e67636172742f6c6963656e7365)](https://packagist.org/packages/bumbummen99/shoppingcart)

This is a fork of [Crisane's LaravelShoppingcart](https://github.com/Crinsane/LaravelShoppingcart) extended with minor features compatible with Laravel 5.8.

Installation
------------

[](#installation)

Install the [package](https://packagist.org/packages/tudorstoicalynx/shoppingcart) through [Composer](http://getcomposer.org/).

Run the Composer require command from the Terminal:

```
composer require tudorstoicalynx/shoppingcart

```

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**

Overview
--------

[](#overview)

Look at one of the following topics to learn more about LaravelShoppingcart

- [Usage](#usage)
- [Collections](#collections)
- [Instances](#instances)
- [Models](#models)
- [Database](#database)
- [Exceptions](#exceptions)
- [Events](#events)
- [Example](#example)

Usage
-----

[](#usage)

The shoppingcart gives you the following methods to use:

### Cart::add()

[](#cartadd)

Adding an item to the cart is really simple, you just use the `add()` method, which accepts a variety of parameters.

In its most basic form you can specify the id, name, quantity, price and weight of the product you'd like to add to the cart.

```
Cart::add('293ad', 'Product 1', 1, 9.99, 550);
```

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.

```
Cart::add('293ad', 'Product 1', 1, 9.99, 550, ['size' => 'large']);
```

**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.

```
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. This way you can just pass the `add()` method a model and the quantity and it will automatically add it to the cart.

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

```
Cart::add($product, 1, ['size' => 'large']);
```

As an optional third parameter you can add options.

```
Cart::add($product, 1, ['size' => 'large']);
```

Finally, you can also add multipe items to the cart at once. You can just pass the `add()` method an array of arrays, or an array of Buyables and they will be added to the cart.

**When adding multiple items to the cart, the `add()` method will return an array of CartItems.**

```
Cart::add([
  ['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]);
```

### Cart::update()

[](#cartupdate)

To update an item in the cart, you'll first need the rowId of the item. Next you can use the `update()` method to update it.

If you simply want to update the quantity, you'll pass the update method the rowId and the new quantity:

```
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

Cart::update($rowId, 2); // Will update the quantity
```

If you want to update more attributes of the item, you can either pass the update method an array or a `Buyable` as the second parameter. This way you can update all information of the item with the given rowId.

```
Cart::update($rowId, ['name' => 'Product 1']); // Will update the name

Cart::update($rowId, $product); // Will update the id, name and price
```

### Cart::remove()

[](#cartremove)

To remove an item for the cart, you'll again need the rowId. This rowId you simply pass to the `remove()` method and it will remove the item from the cart.

```
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

Cart::remove($rowId);
```

### Cart::get()

[](#cartget)

If you want to get an item from the cart using its rowId, you can simply call the `get()` method on the cart and pass it the rowId.

```
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

Cart::get($rowId);
```

### Cart::content()

[](#cartcontent)

Of course you also want to get the carts content. This is where you'll use the `content` method. This method will return a Collection of CartItems which you can iterate over and show the content to your customers.

```
Cart::content();
```

This method will return the content of the current cart instance, if you want the content of another instance, simply chain the calls.

```
Cart::instance('wishlist')->content();
```

### Cart::destroy()

[](#cartdestroy)

If you want to completely remove the content of a cart, you can call the destroy method on the cart. This will remove all CartItems from the cart for the current cart instance.

```
Cart::destroy();
```

### Cart::weight()

[](#cartweight)

The `weight()` method can be used to get the weight total of all items in the cart, given there weight and quantity.

```
Cart::weight();
```

The method will automatically format the result, which you can tweak using the three optional parameters

```
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()

[](#carttotal)

The `total()` method can be used to get the calculated total of all items in the cart, given there price and quantity.

```
Cart::total();
```

The method will automatically format the result, which you can tweak using the three optional parameters

```
Cart::total($decimals, $decimalSeparator, $thousandSeparator);
```

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->total`**

### Cart::tax()

[](#carttax)

The `tax()` method can be used to get the calculated amount of tax for all items in the cart, given there price and quantity.

```
Cart::tax();
```

The method will automatically format the result, which you can tweak using the three optional parameters

```
Cart::tax($decimals, $decimalSeparator, $thousandSeparator);
```

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 tax property `$cart->tax`**

### Cart::subtotal()

[](#cartsubtotal)

The `subtotal()` method can be used to get the total of all items in the cart, minus the total amount of tax.

```
Cart::subtotal();
```

The method will automatically format the result, which you can tweak using the three optional parameters

```
Cart::subtotal($decimals, $decimalSeparator, $thousandSeparator);
```

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()

[](#cartdiscount)

The `discount()` method can be used to get the total discount of all items in the cart.

```
Cart::discount();
```

The method will automatically format the result, which you can tweak using the three optional parameters

```
Cart::discount($decimals, $decimalSeparator, $thousandSeparator);
```

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()

[](#cartinitial)

The `initial()` method can be used to get the total price of all items in the cart before discount.

```
Cart::initial();
```

The method will automatically format the result, which you can tweak using the three optional parameters

```
Cart::initial($decimals, $decimalSeparator, $thousandSeparator);
```

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()

[](#cartcount)

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.

```
Cart::count();
$cart->count();
```

### Cart::search()

[](#cartsearch)

To find an item in the cart, you can use the `search()` method.

**This method was changed on version 2**

Behind the scenes, the method simply uses the filter method of the Laravel Collection class. This means you must pass it a Closure in which you'll specify you search terms.

If you for instance want to find all items with an id of 1:

```
$cart->search(function ($cartItem, $rowId) {
	return $cartItem->id === 1;
});
```

As you can see the Closure will receive two parameters. The first is the CartItem to perform the check against. The second parameter is the rowId of this CartItem.

**The method will return a Collection containing all CartItems that where found**

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)

[](#cartsettaxrowid-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.

```
Cart::setTax($rowId, 21);
$cart->setTax($rowId, 21);
```

### Cart::setGlobalTax($taxRate)

[](#cartsetglobaltaxtaxrate)

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.

```
Cart::setGlobalDiscount(21);
$cart->setGlobalDiscount(21);
```

### Cart::setGlobalDiscount($discountRate)

[](#cartsetglobaldiscountdiscountrate)

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.

```
Cart::setGlobalDiscount(21);
$cart->setGlobalDiscount(21);
```

### Cart::setDiscount($rowId, $taxRate)

[](#cartsetdiscountrowid-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.

```
Cart::setDiscount($rowId, 21);
$cart->setDiscount($rowId, 21);
```

### Buyable

[](#buyable)

For the convenience of faster adding items to cart and their automatic association, your model has to implement the `Buyable` interface. You can use the `CanBeBought` trait to implement the required methods but keep in mind that these will use predefined fields on your model for the required values.

```
