PHPackages                             darthsoup/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. darthsoup/shoppingcart

ActiveLibrary

darthsoup/shoppingcart
======================

Laravel 5 Shoppingcart

2.1.0(5y ago)25.4k1MITPHPPHP ^7.2.5|^8.0CI failing

Since Mar 14Pushed 4y ago1 watchersCompare

[ Source](https://github.com/darthsoup/laravel-shoppingcart)[ Packagist](https://packagist.org/packages/darthsoup/shoppingcart)[ RSS](/packages/darthsoup-shoppingcart/feed)WikiDiscussions main Synced 2mo ago

READMEChangelog (10)Dependencies (6)Versions (21)Used By (0)

Laravel Cart
------------

[](#laravel-cart)

[![Total Downloads](https://camo.githubusercontent.com/52513e19c5b0d59cfa09e1bf2fdb608ddad4ca58b7f6f81d0e96e80012f8cf83/68747470733a2f2f706f7365722e707567782e6f72672f6461727468736f75702f73686f7070696e67636172742f646f776e6c6f616473)](https://packagist.org/packages/darthsoup/shoppingcart)[![License](https://camo.githubusercontent.com/e1cded9e8c538d98071f9d0ea78bc6fac613924e14a7e8f07ef83ba8edc8aab9/68747470733a2f2f706f7365722e707567782e6f72672f6461727468736f75702f73686f7070696e67636172742f6c6963656e7365)](https://packagist.org/packages/darthsoup/shoppingcart)

An easy shoppingcart implementation for Laravel &gt; 6. Based on the work of [Gloudemans\\Shoppingcart](https://github.com/Crinsane/LaravelShoppingcart).

Features
--------

[](#features)

This package for shopping carts provides these features:

- Cart items can have subitems
- Custom hash algorithms for item identifiers
- Individual taxing for single items
- Database Support

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

[](#installation)

To get the latest version, simply require the library using [Composer](http://getcomposer.org/).

```
$ composer require darthsoup/shoppingcart
```

Once installed, you'll need to publish the vendor assets

```
$ php artisan vendor:publish --provider="DarthSoup\Cart\CartServiceProvider" --tag="config"
```

This will create `cart.php` config file.

Usage
-----

[](#usage)

The cart package provides you the following methods to use:

### Add Item

[](#add-item)

```
/**
 * Add a Item to the cart.
 *
 * @param string|array $id Unique ID of the item|Item formatted as array|Array of items
 * @param string $name Name of the item
 * @param int $quantity Item quantity to add to the cart
 * @param float $price Price of one item
 * @param array $options Array of additional options, such as 'size' or 'color'
 */

// Basic form
Cart::add('1', 'Product One', 1, 9.99, ['option_key' => 'option_value']);

// Array form
Cart::add(['id' => 'mail1000', 'name' => 'Mail Package One', 'quantity' => 5, 'price' => 4.99, 'options' => []]);

// Batch method
Cart::add([
    ['id' => '15', 'name' => 'Hamburger', 'quantity' => 1, 'price' => 1.99],
    ['id' => '16', 'name' => 'Cheeseburger', 'quantity' => 1, 'price' => 2.49, 'options' => ['onion' => false]]
]);
```

you also can make Items by make them manually

```
$item = new \DarthSoup\Cart\Item('15', 'Hamburger', 1.99, ['onion' => false]);

Cart::add($item);
```

### Update one Item

[](#update-one-item)

```
Cart::update('rowId', [
    'options' => ['foo' => 'bar']
]);
```

### Get One Cart Item

[](#get-one-cart-item)

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

### Show Cart Content

[](#show-cart-content)

Show the content of the Cart by returning the CartCollection

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

### Empty the cart

[](#empty-the-cart)

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

### Remove one Item or Subitem

[](#remove-one-item-or-subitem)

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

### Total Price of all Items

[](#total-price-of-all-items)

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

### Item Count

[](#item-count)

```
Cart::count();
```

SubItems
--------

[](#subitems)

This package also includes the functionality to add Subitems by adding them to an additional collection in the Item

### Add SubItem

[](#add-subitem)

The `addSubItem` function works basically like `add` but it accepts a parent row Id at the end to add an SubItem to the item.

```
$hamburger = Cart::add('15', 'Hamburger', 1, 1.99, ['onion' => false]);

Cart::addSubItem('99', 'Extra Bacon', 1, 0.99, [], $hamburger->getRowId());
```

### Remove SubItem

[](#remove-subitem)

Just like removing normal ones, just include your subItem `rowId` and it will be removed from the parent

Models
------

[](#models)

A new feature is associating a model with the items in the cart. Let's say you have a `Product` model in your application. With the `associate()` method, you can tell the cart that an item in the cart, is associated to the `Product` model.

That way you can access your model right from the `CartCollection`!

Here is an example:

```
