wildan99 / yii2-shopping-cart
该包的最新版本(1.2.2)没有可用的许可证信息。
添加购物车功能的Yii2扩展
1.2.2
2015-09-08 11:18 UTC
Requires
- php: >=5.4.0
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-21 15:14:37 UTC
README
此扩展为Yii框架2.0添加购物车功能
安装
安装此扩展的最佳方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist wildan99/yii2-shopping-cart "*"
或者
"wildan99/yii2-shopping-cart": "*"
将以下内容添加到您的composer.json文件的require部分。
如何使用
在您的模型中
class Product extends ActiveRecord implements CartPositionInterface { use CartPositionTrait; public function getPrice() { return $this->price; } public function getId() { return $this->id; } }
在您的控制器中
public function actionAddToCart($id) { $cart = new ShoppingCart(); $model = Product::findOne($id); if ($model) { $cart->put($model, 1); return $this->redirect(['cart-view']); } throw new NotFoundHttpException(); }
您还可以将购物车用作全局应用程序组件
[ 'components' => [ 'cart' => [ 'class' => 'yz\shoppingcart\ShoppingCart', 'cartId' => 'my_application_cart', ] ] ]
并按以下方式使用它
\Yii::$app->cart->put($cartPosition, 1);
为了获取购物车中项目数量
$itemsCount = \Yii::$app->cart->getCount();
为了获取购物车中项目总价
$total = \Yii::$app->cart->getCost();
如果您想用作购物车位置的原始模型太重无法存储在会话中,您可以创建一个单独的类实现CartPositionInterface,原始模型可以实现CartPositionProviderInterface
// app\models\Product.php class Product extends ActiveRecord implements CartPositionProviderInterface { public function getCartPosition() { return \Yii::createObject([ 'class' => 'app\models\ProductCartPosition', 'id' => $this->id, ]); } } // app\models\ProductCartPosition.php class ProductCartPosition extends Object implements CartPositionInterface { /** * @var Product */ protected $_product; public $id; public function getId() { return $this->id; } public function getPrice() { return $this->getProduct()->price; } /** * @return Product */ public function getProduct() { if ($this->_product === null) { $this->_product = Product::findOne($this->id); } return $this->_product; } }
这种方式使我们能够为同一产品创建不同的购物车位置,这些位置仅在某个属性上有所不同,例如价格或颜色
// app\models\ProductCartPosition.php class ProductCartPosition extends Object implements CartPositionInterface { public $id; public $price; public $color; //... public function getId() { return md5(serialize([$this->id, $this->price, $this->color])); } //... }
使用折扣
折扣作为附加到购物车或其位置的行为了实现。要使用它们,请按照以下步骤操作
- 将折扣类定义为yz\shoppingcart\DiscountBehavior的子类
// app/components/MyDiscount.php class MyDiscount extends DiscountBehavior { /** * @param CostCalculationEvent $event */ public function onCostCalculation($event) { // Some discount logic, for example $event->discountValue = 100; } }
- 将此行为添加到购物车
$cart->attachBehavior('myDiscount', ['class' => 'app\components\MyDiscount']);
如果折扣不适用于整个购物车,而适用于单个位置,则可以将折扣附加到购物车位置本身
$cart->getPositionById($positionId)->attachBehavior('myDiscount', ['class' => 'app\components\MyDiscount']);
注意,同一行为可以用于购物车和位置类。
- 获取应用折扣后的总价
$total = \Yii::$app->cart->getCost(true);
- 在计算过程中,以下事件被触发
ShoppingCart::EVENT_COST_CALCULATION
在每次计算时触发一次。CartPositionInterface::EVENT_COST_CALCULATION
对于购物车中的每个位置。
您也可以订阅这些事件以执行折扣计算
$cart->on(ShoppingCart::EVENT_COST_CALCULATION, function ($event) { $event->discountValue = 100; });