PHPackages                             crocone/yii2-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. crocone/yii2-cart

ActiveYii2-extension

crocone/yii2-cart
=================

Yii2 Shopping cart

1.4.3.6(6y ago)015MITPHPPHP &gt;=7.1.0

Since May 13Pushed 6y agoCompare

[ Source](https://github.com/crocone/yii2-cart)[ Packagist](https://packagist.org/packages/crocone/yii2-cart)[ RSS](/packages/crocone-yii2-cart/feed)WikiDiscussions master Synced today

READMEChangelog (9)Dependencies (3)Versions (19)Used By (0)

 [ ![](https://avatars0.githubusercontent.com/u/993323) ](https://github.com/yiisoft)

Yii2 Shopping Cart Extension
============================

[](#yii2-shopping-cart-extension)

This extension adds shopping cart for Yii framework 2.0

[![Latest Stable Version](https://camo.githubusercontent.com/f3cb6278b1f608bcbcb92a44369299b0bea774215e773af173ad6103b9ece9d2/68747470733a2f2f706f7365722e707567782e6f72672f63726f636f6e652f796969322d636172742f762f737461626c65)](https://packagist.org/packages/crocone/yii2-cart) [![Total Downloads](https://camo.githubusercontent.com/fc31e15939195a25554a461d870cad9c6292cffac38cbbe6fdbc5d58760aff16/68747470733a2f2f706f7365722e707567782e6f72672f63726f636f6e652f796969322d636172742f646f776e6c6f616473)](https://packagist.org/packages/crocone/yii2-cart) [![License](https://camo.githubusercontent.com/388decc055768ccc14043a44ce1def2695058607aa71aeac008a5ae21244e9ec/68747470733a2f2f706f7365722e707567782e6f72672f63726f636f6e652f796969322d636172742f6c6963656e7365)](https://packagist.org/packages/crocone/yii2-cart)[![Build Status](https://camo.githubusercontent.com/a42be7dbec0a9ecedf3fa02f1b479a97ee19c097e8bf51b434bc293ab877634d/68747470733a2f2f7472617669732d63692e6f72672f63726f636f6e652f796969322d636172742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/crocone/yii2-cart)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/c982e2da9536ecc7e07d4cf449361abf71cef37b7049a35d97e8540e7bdc71d7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63726f636f6e652f796969322d636172742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/crocone/yii2-cart/?branch=master)

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist crocone/yii2-cart "*"

```

or add

```
"crocone/yii2-cart": "*"

```

to the require section of your `composer.json` file.

### Configuration

[](#configuration)

1. Configure the `cart` component:

```
return [
    //....
    'components' => [
        'cart' => [
            'class' => 'crocone\cart\Cart',
            // you can change default storage class as following:
            'storageClass' => [
                'class' => 'crocone\cart\storage\DatabaseStorage',
                // you can also override some properties
                'deleteIfEmpty' => true
            ]
        ],
    ]
];
```

2. Create the Product Model that implements an `CartItemInterface`:

```
class ProductModel extends ActiveRecord implements CartItemInterface
{

    public function getPrice()
    {
        return $this->price;
    }

    public function getLabel()
    {
        return $this->name;
    }

    public function getUniqueId()
    {
        return $this->id;
    }
}
```

> If you use the crocone\\cart\\storage\\DatabaseStorage as `storageClass` then you need to apply the following migration:

```
php yii migrate --migrationPath=@vendor/crocone/yii2-cart/migrations
```

### Using the shopping cart

[](#using-the-shopping-cart)

Operations with the shopping cart are very straightforward when using a models that implement one of the two cart interfaces. The cart object can be accessed under `\Yii::$app->cart` and can be overridden in configuration if you need to customize it.

```
// access the cart from "cart" subcomponent
$cart = \Yii::$app->cart;

// Product is an AR model implementing CartProductInterface
$product = Product::findOne(1);

// add an item to the cart
$cart->add($product);

// returns the sum of all 'vat' attributes (or return values of getVat()) from all models in the cart.
$totalVat = $cart->getAttributeTotal('vat');

// clear the cart
$cart->clear();
```

#### View Cart Items

[](#view-cart-items)

You can use the `CartGrid` widget for generate table with cart items as following:

```
