devanych / yii2-cart
为Yii2的购物车
Requires
- php: >=5.6
- yiisoft/yii2: *
Requires (Dev)
- phpunit/phpunit: ~6.0
README
此扩展为Yii框架2.0添加购物车功能
在此处提供了俄语的详细说明指南。
安装
安装此扩展的首选方式是通过Composer
运行
php composer.phar require devanych/yii2-cart "*"
或在您的composer.json
文件的require
部分中添加
devanych/yii2-cart: "*"
。
配置
配置cart
组件(默认值如下所示)
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', ], ], ] //... ];
除了devanych\cart\storage\SessionStorage
之外,还有devanych\cart\storage\CookieStorage
和devanych\cart\storage\DbSessionStorage
。您还可以创建自己的存储,您需要实现接口devanych\cart\storage\StorageInterface
。
DbSessionStorage
使用SessionStorage
处理未授权用户,使用数据库处理授权用户。
如果您将
devanych\cart\storage\DbSessionStorage
作为storageClass
使用,则需要应用以下迁移
php yii migrate --migrationPath=@vendor/devanych/yii2-cart/migrations
devanych\cart\calculators\SimpleCalculator
生成购物车中商品的总成本和总数量。如果您需要进行带有折扣或其他计算的运算,可以创建自己的计算器,通过实现接口devanych\cart\calculators\CalculatorInterface
。
设置params
数组
-
key
- 对于会话和Cookie。 -
expire
- Cookie的有效期。 -
productClass
- 产品类是一个ActiveRecord模型。 -
productFieldId
- 产品模型的id
字段名称。 -
productFieldPrice
- 产品模型的price
字段名称。
支持同一网站上的多个购物车
//... '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', ], ], //...
用法
您可以在应用程序的任何地方使用Yii::$app->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();
使用购物车项
// 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);
通过使用方法
getProduct()
,您可以访问产品的所有属性和方法。
$product = $item->getProduct(); echo $product->name;