PHPackages                             imgul/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. imgul/shoppingcart

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

imgul/shoppingcart
==================

Laravel Shoppingcart

v1.0.0(1y ago)010MITPHP

Since Jun 10Pushed 1y agoCompare

[ Source](https://github.com/imgul/LaravelShoppingcart)[ Packagist](https://packagist.org/packages/imgul/shoppingcart)[ RSS](/packages/imgul-shoppingcart/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (7)Versions (2)Used By (0)

Attention
=========

[](#attention)

Due to the difficulty experienced by certain individuals and companies in comprehending the [copyleft provision of the MIT license](https://github.com/bumbummen99/LaravelShoppingcart/blob/1c50fb564826b6ee748d2d9afad7447b6884166e/LICENSE#L12), as evidenced by the unauthorized relicensing of my work and that of others, I have opted to terminate all further development and assistance for this repository. Given that this package is intended for use in e-commerce projects, it is expected that the necessary resources would be available for the development and support of your own solutions.

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

[](#laravelshoppingcart)

[![CI Code Checks](https://github.com/bumbummen99/LaravelShoppingcart/workflows/CI%20Code%20Checks/badge.svg?branch=master)](https://github.com/bumbummen99/LaravelShoppingcart/workflows/CI%20Code%20Checks/badge.svg?branch=master)[![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 [Crinsane's LaravelShoppingcart](https://github.com/Crinsane/LaravelShoppingcart) extended with minor features compatible with Laravel 8+. An example integration can be [found here](https://github.com/bumbummen99/LaravelShoppingcartDemo).

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

[](#installation)

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

```

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

You definitely should publish the `config` file and take a look at it.

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

```

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

Updates
-------

[](#updates)

As of version **4.2.0** this package does, when being used with PostgreSQL, encode the cart content to base64 before storing into database due to an [issue with saving values including zero bytes](https://github.com/bumbummen99/LaravelShoppingcart/pull/167). Please consider clearing your cart table in case you are upgrading using PostgreSQL from a version **&lt;4.2.0**.

Table of Contents
-----------------

[](#table-of-contents)

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

- [Important note](#important-note)
- [Usage](#usage)
- [Collections](#collections)
- [Instances](#instances)
- [Models](#models)
- [Database](#database)
- [Calculators](#calculators)
- [Exceptions](#exceptions)
- [Events](#events)
- [Example](#example)
- [Contributors](#contributors)

Important note
--------------

[](#important-note)

As all the shopping cart that calculate prices including taxes and discount, also this module could be affected by the "totals rounding issue" ([\*](https://stackoverflow.com/questions/13529580/magento-tax-rounding-issue)) due to the decimal precision used for prices and for the results. In order to avoid (or at least minimize) this issue, in the Laravel shoppingcart package the totals are calculated using the method **"per Row"** and returned already rounded based on the number format set as default in the config file (cart.php). Due to this **WE DISCOURAGE TO SET HIGH PRECISION AS DEFAULT AND TO FORMAT THE OUTPUT RESULT USING LESS DECIMAL** Doing this can lead to the rounding issue.

The base price (product price) is left not rounded.

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 would like to update options of an item inside the cart,

```
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

Cart::update($rowId, ['options'  => ['size' => 'small']]); // Will update the size option with new value
```

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 their 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 applying discount and taxes.

It could be deprecated in the future. **When rounded could be affected by the rounding issue**, use it carefully or use Cart::priceTotal()

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

### Cart::priceTotal()

[](#cartpricetotal)

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

```
Cart::priceTotal();
```

The method return the result rounded based on the default number format, but you can tweak using the three optional parameters

```
Cart::priceTotal($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::setGlobalTax(21);
$cart->setGlobalTax(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(50);
$cart->setGlobalDiscount(50);
```

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

```
