PHPackages                             irice/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. [Framework](/categories/framework)
4. /
5. irice/yii2-cart

ActiveYii2-extension[Framework](/categories/framework)

irice/yii2-cart
===============

Yii2 Shopping cart

1.4(7y ago)0114MITPHPPHP &gt;=7.1.0

Since May 13Pushed 4y agoCompare

[ Source](https://github.com/iridance/yii2-cart)[ Packagist](https://packagist.org/packages/irice/yii2-cart)[ RSS](/packages/irice-yii2-cart/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (3)Versions (11)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/dda241ff5997d387bcb058b5e60b4e21f9e53dc8194bb269d706ea30f2796473/68747470733a2f2f706f7365722e707567782e6f72672f796969326d6f642f796969322d636172742f762f737461626c65)](https://packagist.org/packages/yii2mod/yii2-cart) [![Total Downloads](https://camo.githubusercontent.com/101b643aa218bc2b88514286f30bb7555cd34fc77bc56a5140d1ae2d93e7e996/68747470733a2f2f706f7365722e707567782e6f72672f796969326d6f642f796969322d636172742f646f776e6c6f616473)](https://packagist.org/packages/yii2mod/yii2-cart) [![License](https://camo.githubusercontent.com/c35d43deec60229cf59dcd709ba45e9eee2959a4bab5dce550d8cdd61cde8311/68747470733a2f2f706f7365722e707567782e6f72672f796969326d6f642f796969322d636172742f6c6963656e7365)](https://packagist.org/packages/yii2mod/yii2-cart)[![Build Status](https://camo.githubusercontent.com/d677abf6c55e66714d05895b635c28071f74739f136dee8a550dc5585ea41ca6/68747470733a2f2f7472617669732d63692e6f72672f796969326d6f642f796969322d636172742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/yii2mod/yii2-cart)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/5774c5a90ce249f7087427389eb2e684c66f26c5ff94d85b222121410cae27e3/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f796969326d6f642f796969322d636172742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/yii2mod/yii2-cart/?branch=master)

Support us
----------

[](#support-us)

Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/yii2mod). All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

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

[](#installation)

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

Either run

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

```

or add

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

```

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

### Configuration

[](#configuration)

1. Configure the `cart` component:

```
return [
    //....
    'components' => [
        'cart' => [
            'class' => 'yii2mod\cart\Cart',
            // you can change default storage class as following:
            'storageClass' => [
                'class' => 'yii2mod\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 irice\\cart\\storage\\DatabaseStorage as `storageClass` then you need to apply the following migration:

```
php yii migrate --migrationPath=@vendor/yii2mod/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:

```
