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

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

devanych/yii2-cart
==================

Shopping cart for Yii2

v1.2.5(6y ago)2011.2k↓27.8%12BSD-3-ClausePHPPHP &gt;=5.6CI failing

Since Jun 6Pushed 6y ago2 watchersCompare

[ Source](https://github.com/devanych/yii2-cart)[ Packagist](https://packagist.org/packages/devanych/yii2-cart)[ RSS](/packages/devanych-yii2-cart/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (9)Used By (0)

Yii2 shopping cart
==================

[](#yii2-shopping-cart)

This extension adds shopping cart for Yii framework 2.0

Guide with a detailed description in Russian language [here](https://github.com/devanych/yii2-cart/blob/master/docs/guide-ru.md).

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

[](#installation)

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

Either run

```
php composer.phar require devanych/yii2-cart "*"

```

or add

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

```

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

Configuration
-------------

[](#configuration)

Configure the `cart` component (default values are shown):

```
return [
    //...
    'components' => [
        //...
        'cart' => [
            'class' => 'devanych\cart\Cart',
            'storageClass' => 'devanych\cart\storage\SessionStorage',
            'calculatorClass' => 'devanych\cart\calculators\SimpleCalculator',
            'params' => [
                'key' => 'cart',
                'expire' => 604800,
                'productClass' => 'app\model\Product',
                'productFieldId' => 'id',
                'productFieldPrice' => 'price',
            ],
        ],
    ]
    //...
];
```

In addition to `devanych\cart\storage\SessionStorage`, there is also `devanych\cart\storage\CookieStorage` and `devanych\cart\storage\DbSessionStorage`. It is possible to create your own storage, you need to implement the interface `devanych\cart\storage\StorageInterface`.

`DbSessionStorage` uses `SessionStorage` for unauthorized users and database for authorized.

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

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

`devanych\cart\calculators\SimpleCalculator` produces the usual calculation of the total cost and total quantity of items in the cart. If you need to make a calculation with discounts or something else, you can create your own calculator by implementing the interface `devanych\cart\calculators\CalculatorInterface`.

Setting up the `params` array:

- `key` - For Session and Cookie.
- `expire` - Cookie life time.
- `productClass` - Product class is an ActiveRecord model.
- `productFieldId` - Name of the product model `id` field.
- `productFieldPrice` - Name of the product model `price` field.

#### Supporting multiple shopping carts to same website:

[](#supporting-multiple-shopping-carts-to-same-website)

```
//...
'cart' => [
    'class' => 'devanych\cart\Cart',
    'storageClass' => 'devanych\cart\storage\SessionStorage',
    'calculatorClass' => 'devanych\cart\calculators\SimpleCalculator',
    'params' => [
        'key' => 'cart',
        'expire' => 604800,
        'productClass' => 'app\model\Product',
        'productFieldId' => 'id',
        'productFieldPrice' => 'price',
    ],
],
'favorite' => [
    'class' => 'devanych\cart\Cart',
    'storageClass' => 'devanych\cart\storage\DbSessionStorage',
    'calculatorClass' => 'devanych\cart\calculators\SimpleCalculator',
    'params' => [
        'key' => 'favorite',
        'expire' => 604800,
        'productClass' => 'app\models\Product',
        'productFieldId' => 'id',
        'productFieldPrice' => 'price',
    ],
],
//...
```

Usage
-----

[](#usage)

You can get the shopping cart component anywhere in the app using `Yii::$app->cart`.

Using cart:

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

// Get component of the cart
$cart = \Yii::$app->cart;

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

// Adding item quantity in the cart
$cart->plus($product->id, $quantity);

// Change item quantity in the cart
$cart->change($product->id, $quantity);

// Removes an items from the cart
$cart->remove($product->id);

// Removes all items from the cart
$cart->clear();

// Get all items from the cart
$cart->getItems();

// Get an item from the cart
$cart->getItem($product->id);

// Get ids array all items from the cart
$cart->getItemIds();

// Get total cost all items from the cart
$cart->getTotalCost();

// Get total count all items from the cart
$cart->getTotalCount();
```

#### Using cart items:

[](#using-cart-items)

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

// Get component of the cart
$cart = \Yii::$app->cart;

// Get an item from the cart
$item = $cart->getItem($product->id);

// Get the id of the item
$item->getId();

// Get the price of the item
$item->getPrice();

// Get the product, AR model
$item->getProduct();

// Get the cost of the item
$item->getCost();

// Get the quantity of the item
$item->getQuantity();

// Set the quantity of the item
$item->setQuantity($quantity);
```

> By using method `getProduct()`, you have access to all the properties and methods of the product.

```
$product = $item->getProduct();

echo $product->name;
```

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 93.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~71 days

Recently: every ~118 days

Total

8

Last Release

2395d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7ab952aaa5ac8f82d6e96ef74c2f69082674a97eb3bc62f9d96c7304b2b4b082?d=identicon)[devanych](/maintainers/devanych)

---

Top Contributors

[![devanych](https://avatars.githubusercontent.com/u/20116244?v=4)](https://github.com/devanych "devanych (15 commits)")[![shahjadin](https://avatars.githubusercontent.com/u/45035428?v=4)](https://github.com/shahjadin "shahjadin (1 commits)")

---

Tags

yii2yii2-cartyii2-extensionyii2-shopping-cartyii2yii2-extensionyii2-cartyii2-shopping-cart

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/devanych-yii2-cart/health.svg)

```
[![Health](https://phpackages.com/badges/devanych-yii2-cart/health.svg)](https://phpackages.com/packages/devanych-yii2-cart)
```

###  Alternatives

[yiisoft/yii2-twig

The Twig integration for the Yii framework

1431.9M32](/packages/yiisoft-yii2-twig)[skeeks/cms

SkeekS CMS — control panel and tools based on php framework Yii2

13825.6k46](/packages/skeeks-cms)[tecnocen/yii2-formgenerator

Yii 2 Library to configure form generator

145.7k](/packages/tecnocen-yii2-formgenerator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
