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

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

kingpabel/shoppingcart
======================

Laravel 5 Shoppingcart

1.4(10y ago)81.5k4MITPHPPHP &gt;=5.4.0

Since Mar 14Pushed 10y ago3 watchersCompare

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

READMEChangelog (2)Dependencies (3)Versions (8)Used By (0)

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

[](#laravelshoppingcart)

[![Build Status](https://camo.githubusercontent.com/31f749a6cec3380ce7119130a5cb6eff767d2bc4d5426d94444cd01dcf156726/68747470733a2f2f7472617669732d63692e6f72672f6b696e67706162656c2f4c61726176656c53686f7070696e67636172742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/kingpabel/LaravelShoppingcart)[![Total Downloads](https://camo.githubusercontent.com/35dee9856707ce32a704234c5ef7dfd982693b8bfc3dc17fd1af8e64718a515c/68747470733a2f2f706f7365722e707567782e6f72672f676c6f7564656d616e732f73686f7070696e67636172742f646f776e6c6f6164732e706e67)](https://packagist.org/packages/kingpabel/shoppingcart)

A simple shoppingcart implementation for Laravel 4.

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

[](#installation)

Install the package through [Composer](http://getcomposer.org/). Edit your project's `composer.json` file by adding:

### From Laravel 4.2 to Laravel 5.2

[](#from-laravel-42-to-laravel-52)

```
"require": {
    "kingpabel/shoppingcart": "~1.4"
}
```

Next, run the Composer update command from the Terminal:

```
composer update

```

Now all you have to do is add the service provider of the package and alias the package. To do this open your `app/config/app.php` file.

Add a new line to the `service providers` array:

```
'Kingpabel\Shoppingcart\ShoppingcartServiceProvider'

```

And finally add a new line to the `aliases` array:

```
'Cart'            => 'Kingpabel\Shoppingcart\Facades\Cart',

```

Now you're ready to start using the shoppingcart in your application.

Overview
--------

[](#overview)

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

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

Usage
-----

[](#usage)

The shoppingcart gives you the following methods to use:

**Cart::add()**

```
 /**
    * Add a row to the cart
    *
    * @param string|array $id   Unique ID of the item|Item formated as array|Array of items
    * @param string       $name Name of the item
    * @param int          $qty Item qty 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('293ad', 'Product 1', 1, 9.99, 0.00, array('size' => 'large'));

// Array form
Cart::add(array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'discount' => 0.00, 'options' => array('size' => 'large')));

// Batch method
Cart::add(array(
  array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00, 'discount' => 0.00,),
  array('id' => '4832k', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00,, 'discount' => 0.00, 'options' => array('size' => 'large'))
));
```

**Cart::update()**

```
/**
 * Update the quantity of one row of the cart
 *
 * @param  string        $rowId       The rowid of the item you want to update
 * @param  integer|Array $attribute   New quantity of the item|Array of attributes to update
 * @return boolean
 */
 $rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

Cart::update($rowId, 2);

OR

Cart::update($rowId, array('name' => 'Product 1'));
```

**Cart::remove()**

```
/**
 * Remove a row from the cart
 *
 * @param  string  $rowId The rowid of the item
 * @return boolean
 */

 $rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

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

**Cart::get()**

```
/**
 * Get a row of the cart by its ID
 *
 * @param  string $rowId The ID of the row to fetch
 * @return CartRowCollection
 */

$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

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

**Cart::content()**

```
/**
 * Get the cart content
 *
 * @return CartCollection
 */

Cart::content();
```

**Cart::destroy()**

```
/**
 * Empty the cart
 *
 * @return boolean
 */

Cart::destroy();
```

**Cart::total()**

```
/**
 * Total amount of cart
 *
 * @return float
 */

Cart::total();
```

**Cart::subtotal()**

```
/**
 * Sub total amount of cart
 *
 * @return float
 */

Cart::subtotal();
```

**Cart::discount()**

```
/**
 * Discount of cart
 *
 * @return float
 */

Cart::discount();
```

**Cart::setCustomDiscount(5.00)**

```
/**
 * @param $amount
 * @return bool
 */

Cart::setCustomDiscount(5.00);
```

**Cart::customDiscount()**

```
/**
 * Custom discount of cart
 *
 * @return float
 */

Cart::customDiscount();
```

**Cart::count()**

```
/**
 * Get the number of items in the cart
 *
 * @param  boolean $totalItems Get all the items (when false, will return the number of rows)
 * @return int
 */

 Cart::count();      // Total items
 Cart::count(false); // Total rows
```

**Cart::search()**

```
/**
 * Search if the cart has a item
 *
 * @param  Array  $search An array with the item ID and optional options
 * @return Array|boolean
 */

 Cart::search(array('id' => 1, 'options' => array('size' => 'L'))); // Returns an array of rowid(s) of found item(s) or false on failure
```

Collections
-----------

[](#collections)

As you might have seen, the `Cart::content()` and `Cart::get()` methods both return a Collection, a `CartCollection` and a `CartRowCollection`.

These Collections extends the 'native' Laravel 4 Collection class, so all methods you know from this class can also be used on your shopping cart. With some addition to easily work with your carts content.

Instances
---------

[](#instances)

Now the packages also supports multiple instances of the cart. The way this works is like this:

You can set the current instance of the cart with `Cart::instance('newInstance')`, at that moment, the active instance of the cart is `newInstance`, so when you add, remove or get the content of the cart, you work with the `newInstance` instance of the cart. If you want to switch instances, you just call `Cart::instance('otherInstance')` again, and you're working with the `otherInstance` again.

So a little example:

```
Cart::instance('shopping')->add('192ao12', 'Product 1', 1, 9.99);

// Get the content of the 'shopping' cart
Cart::content();

Cart::instance('wishlist')->add('sdjk922', 'Product 2', 1, 19.95, array('size' => 'medium'));

// Get the content of the 'wishlist' cart
Cart::content();

// If you want to get the content of the 'shopping' cart again...
Cart::instance('shopping')->content();

// And the count of the 'wishlist' cart again
Cart::instance('wishlist')->count();
```

N.B. Keep in mind that the cart stays in the last set instance for as long as you don't set a different one during script execution.

N.B.2 The default cart instance is called `main`, so when you're not using instances,`Cart::content();` is the same as `Cart::instance('main')->content()`.

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 new `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 `CartRowCollection`!

Here is an example:

```
