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

ActiveYii2-extension

vsevolod-ryzhov/yii2-cart
=========================

Yii2 cart component

v0.9(5y ago)012MITPHPPHP &gt;=7.1CI failing

Since May 26Pushed 5y ago1 watchersCompare

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

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

Yii2 cart component
===================

[](#yii2-cart-component)

Yii2 cart component for eCommerce web applications

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

[](#installation)

Via Composer

```
composer require vsevolod-ryzhov/yii2-cart

```

Usage
-----

[](#usage)

Use CombinedStorage if you want to use all provided functionality.

1. To use session as cart storage:

```
// setup Cart component using included SessionStorage class
class Init implements BootstrapInterface
{
    public function bootstrap($app): void
    {
        $container = Yii::$container;

        $container->setSingleton(Cart::class, [], [
            new SessionStorage('cart', $app->session), new BaseCost()
        ]);
    }
}

// Product class must implement ProductInterface
class Product implements ProductInterface
{
    public function getId()
    {
        return $this->id;
    }

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

// inject component in your controller
class CartController extends Controller
{
    /**
     * @var Cart
     */
    private $cart;

    public function __construct($id, $module, Cart $cart, $config = [])
    {
        parent::__construct($id, $module, $config);
        $this->cart = $cart;
    }

    public function actionAdd($id, $count = 1)
    {
        $product = Product::findOne($id);
        $this->cart->add($product,  $count);
    }
}
```

2. To use database storage (for authenticated users) and session storage (for guests):

```
// create converter class which extends included AbstractProductConverter
// this class must convert your Product model class to CartItem class
// this class is used in Init class on DatabaseStorage initialization
class ProductToCartConverter extends AbstractProductConverter
{
    public function convertProductToCartItem($product, int $quantity): CartItem
    {
        /* @var $product Product */
        return new CartItem($product, $quantity);
    }
}
// setup Cart component using included SessionStorage class
class Init implements BootstrapInterface
{
    public function bootstrap($app): void
    {
        $container = Yii::$container;
        if ($app->user->isGuest) {
            $container->setSingleton(Cart::class, [], [
                new SessionStorage('cart', $app->session), new BaseCost()
            ]);
        } else {
            // you can specify database options for DatabaseStorage class (cartItemsTable & etc)
            $container->setSingleton(Cart::class, [], [
                new DatabaseStorage($app->user->id, $app->db, new ProductsQuery(Product::class), new ProductToCartConverter), new BaseCost()
            ]);
        }
    }
}

// Query class for Product model must extends included AbstractProductQuery
class ProductsQuery extends AbstractProductQuery
{
    /**
     * @return ProductsQuery
     */
    public function canBuy(): ActiveQuery
    {
        // query product which user can buy
        return $this->active()->available();
    }
}
```

3. To use CookieStorage:

```
// use ProductToCartConverter from previews sample
class Init implements BootstrapInterface
{
    $container->setSingleton(Cart::class, [], [
        new CookieStorage(
            new CookieStorageSettings('cart', 3600),
            $app->request->cookies,
            $app->response->cookies,
            new ProductsQuery(Product::class),
            new ProductToCartConverter
        ),
        new BaseCost()
    ]);
}
```

4. CombinedStorage (CookieStorage + DatabaseStorage):

```
class Init implements BootstrapInterface
{
    public function bootstrap($app): void
    {
        $container = Yii::$container;
        $container->setSingleton(Cart::class, [], [
            new CombinedStorage(
                $app->user,
                $app->db,
                new CookieStorageSettings('cart', 3600),
                $app->request->cookies,
                $app->response->cookies,
                new ProductsQuery(Product::class),
                new ProductToCartConverter
            ),
            new BaseCost()
        ]);
    }
}
```

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~6 days

Recently: every ~0 days

Total

9

Last Release

2123d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3246627?v=4)[Vsevolod Ryzhov](/maintainers/vsevolod-ryzhov)[@vsevolod-ryzhov](https://github.com/vsevolod-ryzhov)

---

Top Contributors

[![vsevolod-ryzhov](https://avatars.githubusercontent.com/u/3246627?v=4)](https://github.com/vsevolod-ryzhov "vsevolod-ryzhov (11 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M2.6k](/packages/craftcms-cms)

PHPackages © 2026

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