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

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

asyou99/yii2-cart
=================

Yii2 extension that adds shopping cart functions

v2.2(10y ago)0344↓66.7%1MITPHPPHP &gt;=5.4.0

Since Jan 15Pushed 8y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (7)Used By (0)

Shopping cart for Yii 2
=======================

[](#shopping-cart-for-yii-2)

This extension is improvisation of omnilight/yii2-shopping-cart. It's add shopping cart systems for Yii framework 2.0. It have feature for save to some medium, they are session (default), cookie, localStorage, database, and multiple storage.

What's is the meaning of the multiple storage? It's feature that can handle two storage where it will save cart data to storage 1 if user is guest, and save to storage 2 if user is logged user.

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

[](#installation)

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

Either run

```
php composer.phar require --prefer-dist asyou99/yii2-cart "dev-master"

```

or add

```
"asyou99/yii2-cart": "dev-master"
```

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

If You plan to save cart data into database, so You should create table cart.

```
CREATE TABLE `cart` (
  `id` varchar(255) NOT NULL,
  `user_id` int(11) DEFAULT NULL,
  `name` varchar(255) NOT NULL,
  `value` text NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `cart`
  ADD PRIMARY KEY (`id`);

```

or use migration

```
yii migrate --migrationPath=@asyou99/cart/migrations

```

How to use
----------

[](#how-to-use)

In your model:

```
class Product extends ActiveRecord implements ItemInterface
{
    use ItemTrait;

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

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

In your controller:

```
	public function actionCreate($id)
    {
        $product = Product::findOne($id);
        if ($product) {
            \Yii::$app->cart->create($product);
            $this->redirect(['index']);
        }
    }
    public function actionIndex()
    {
        $cart = \Yii::$app->cart;
        $products = $cart->getItems();
        $total = $cart->getCost();
        return $this->render('index', [
            'products' => $products,
            'total' => $total,
        ]);
    }
    public function actionDelete($id)
    {
        $product = Product::findOne($id);
        if ($product) {
            \Yii::$app->cart->delete($product);
            $this->redirect(['index']);
        }
    }
    public function actionUpdate($id, $quantity)
    {
        $product = Product::findOne($id);
        if ($product) {
            \Yii::$app->cart->update($product, $quantity);
            $this->redirect(['index']);
        }
    }

	public function actionCheckout(){
		\Yii::$app->cart->checkOut(false);
		$this->redirect(['index']);
	}
```

Also you can use cart as global application component:

```
[
    'components' => [
        'cart' => [
			'class' => 'asyou99\cart\Cart',
		],
    ]
]
```

Possible values of storage are

- asyou99\\cart\\CookieStorage
- asyou99\\cart\\SessionStorage
- asyou99\\cart\\LocalStorage
- asyou99\\cart\\DatabaseStorage
- asyou99\\cart\\MultipleStorage

Example configuration for MultipleStorage.

```
[
    'components' => [
        'cart' => [
			'class' => 'asyou99\cart\Cart',
			'storage' => [
				'class' => 'asyou99\cart\MultipleStorage',
				'storages' => [
					['class' => 'asyou99\cart\SessionStorage'],
					[
						'class' => 'asyou99\cart\DatabaseStorage',
						'table' => 'cart',
					],
				],
			]
		],
    ]
]
```

If You use Multiple Storage, so You should add bootstrap in configuration file:

```
    'bootstrap' => [
		...
		'asyou99\cart\CartBootstrap'
	],
```

Or You can create and use Your own storageClass, it's should extends abstract class of asyou99\\cart\\Storage. It is look like :

```
