asyou99/yii2-cart
Composer 安装命令:
composer require asyou99/yii2-cart
包简介
Yii2 extension that adds shopping cart functions
关键字:
README 文档
README
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
The preferred way to install this extension is through composer.
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
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 :
<?php
namespace app\foo;
use asyou99\cart\Storage;
class ExampleStorage extends Storage
{
public function read(Cart $cart)
{
// read cart data
}
public function write(Cart $cart)
{
// write cart data
}
public function lock($drop, Cart $cart)
{
// lock cart data, only for db
}
}
And use it in the following way:
\Yii::$app->cart->create($product, 1);
In order to get number of items in the cart:
$itemsCount = \Yii::$app->cart->getCount();
In order to get total cost of items in the cart:
$total = \Yii::$app->cart->getCost();
If user have finished, and do checkout, so wen use following code
\Yii::$app->cart->removeAll(); // will remove data // or \Yii::$app->cart->checkOut(); // will remove data // or \Yii::$app->cart->checkOut(false); // will keep data, only update status to 1 and regenerate session ID
Using discounts
Discounts are implemented as behaviors that could attached to the cart or it's items. To use them, follow this steps:
- Define discount class as a subclass of asyou99\cart\DiscountBehavior
// app/components/MyDiscount.php class MyDiscount extends DiscountBehavior { /** * @param CostCalculationEvent $event */ public function onCostCalculation($event) { // Some discount logic, for example $event->discountValue = 100; } }
- Add this behavior to the cart:
$cart->attachBehavior('myDiscount', ['class' => 'app\components\MyDiscount']);
If discount is suitable not for the whole cart, but for the individual item, than it is possible to attach discount to the cart position itself:
$cart->getItemById($itemId)->attachBehavior('myDiscount', ['class' => 'app\components\MyDiscount']);
Note, that the same behavior could be used for both cart and item classes.
- To get total cost with discount applied:
$total = \Yii::$app->cart->getCost(true);
- During the calculation the following events are triggered:
Cart::EVENT_COST_CALCULATIONonce per calculation.ItemInterface::EVENT_COST_CALCULATIONfor each item in the cart.
You can also subscribe on this events to perform discount calculation:
$cart->on(Cart::EVENT_COST_CALCULATION, function ($event) { $event->discountValue = 100; });
asyou99/yii2-cart 适用场景与选型建议
asyou99/yii2-cart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 354 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「yii」 「shopping cart」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 asyou99/yii2-cart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 asyou99/yii2-cart 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 asyou99/yii2-cart 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
Laravel 11 and above Shopping cart
Admin Hub for GetCandy. A modern headless e-commerce solution for Laravel PHP framework.
Priveate for SkeekS CMS
universal shopping basket
Google Shopping Feed API (with ns problem fixed)
统计信息
- 总下载量: 354
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-12-09