PHPackages                             damjangkae/cart - 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. [API Development](/categories/api)
4. /
5. damjangkae/cart

ActiveLibrary[API Development](/categories/api)

damjangkae/cart
===============

E-Commerce Cart Package

07PHP

Since Apr 11Pushed 8y ago1 watchersCompare

[ Source](https://github.com/damjangkae/cart)[ Packagist](https://packagist.org/packages/damjangkae/cart)[ RSS](/packages/damjangkae-cart/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Cart
============

[](#laravel-cart)

A simple cart built for Laravel.

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

[](#installation)

1. Download this repository and place in `packages`
2. Add the path into `composer.json`

```
composer require damjangkae/cart
```

3. Add these following lines to `config/app.php`.

- At `providers` array:

```
Damjangkae\Cart\CartServiceProvider::class
```

- At `aliases` array:

```
'Cart' => Damjangkae\Cart\Facades\Cart::class
```

4. Do `composer dump-autoload`.

Done.

---

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)

Usage
-----

[](#usage)

Here is the methods provided:

### Cart::add()

[](#cartadd)

```
$book = Book::find(1);
Cart::add($book);
```

By the model `Book` must be implemented `Damjangkae\Cart\Contracts\Buyable` which required methods:

```
public function getIdentifier();
public function getPrice(): float;
```

The function `getIdentifier()` expect model to return ID or something that unique your item.

**Another optional way to use `add()`.**

With optional parameters:

```
Cart::add($book, 2, 100, ['author' => 'John Doe']);
```

or parameters in array as secondary argument:

```
Cart::add($book, [
    'quantity' => 2,
    'price' => 100,
    'attributes' => ['author' => 'John Doe']
]);
```

### Cart::update()

[](#cartupdate)

To update an item in the cart, you must specific the identifier.

```
$identifier = '9790404436093';
Cart::update($identifier, 2);
```

The existing data will be overwritten with new one by using `update()` which different with `add()` that rely on existing one. Example:

```
Cart::add($book1, 2);
Cart::update($book1, 3);
// The total will be 3

Cart::add($book2, 2);
Cart::add($book2, 3);
// The total will be 5
```

**Another way to use `update()`.**

Same as you do with `add()`.

```
Cart::update($identifier, ['price' => 900]);
```

### Cart::remove()

[](#cartremove)

```
$identifier = '9790404436093';
Cart::remove($identifier);
```

**Notice: Updating item quantity to 0 is share the same result with `remove()`.**

### Cart::find()

[](#cartfind)

Easy as same as `update()` and `remove()`

```
$identifier = '9790404436093';

Cart::find($identifier);
```

`Damjangkae\Cart\Exceptions\ItemNotFoundException` will be thrown if item not exists.

### Cart::items()

[](#cartitems)

Return collection of items in cart

```
Cart::items();
```

### Cart::destroy()

[](#cartdestroy)

Delete the cart object.

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

### Cart::subtotal()

[](#cartsubtotal)

Return the subtotal price.

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

### Cart::total()

[](#carttotal)

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

### Cart::count()

[](#cartcount)

Return the item count by type. **2 books and 3 chairs will return 2**

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

### Cart::quantity()

[](#cartquantity)

Return the item count by all of quantity. **2 books and 3 chairs will return 5**

```
Cart::quantity();
```

### Cart::isEmpty()

[](#cartisempty)

Return `true` if cart has no items.

```
Cart::isEmpty();
```

### Cart::search()

[](#cartsearch)

Passed closure function and you do your magic.

```
Cart::search(function (CartItem $cartItem) {
	return $cartItem->price > 1000;
});
```

**Notice: Result returned in collection.**

---

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

[](#collections)

Most of methods return as collection (`Illuminate\Support\Collection`) So you know what to do.

```
Cart::items()->first();
```

```
Cart::search(function (CartItem $cartItem) {
	return $cartItem->price > 1000;
})->count();
```

Please see laravel's collection for more info:

---

Instances
---------

[](#instances)

You may wish to have a multiple instance of the cart in same session, here is the solution.

```
$book1 = Book::find(1);
$book2 = Book::find(2);

Cart::instance('gift')->add($book1);
Cart::instance('wishlist')->add($book2);

Cart::instance('gift')->items()->first(); // book1
Cart::instance('wishlist')->items()->first(); // book2
```

**Notice: If the instance name not set, the default will be `default` prepend with `cart_`. So If you set instance name as `wishlist` the session name will be `cart_whishlist`.**

---

Conditions
----------

[](#conditions)

Every e-commerce have a promotion so let the Cart do the magic behind the scene.

#### First: Create your condition class.

[](#first-create-your-condition-class)

If your condition going to affect with **total price** make sure you implement `Damjangkae\Cart\Conditions\TotalAffectable`, but if your condition going to affect with **items** you must implement `Damjangkae\Cart\Conditions\ItemAffectable`.

**Example:** You'd like to have a 10% discount if customer has subtotal more than 500.

```
