cdev38399 / yii2-shopping-cart-private
此包最新版本(1.2.5)没有提供许可证信息。
为Yii2添加购物车功能的扩展
1.2.5
2016-03-29 06:35 UTC
Requires
- php: >=5.4.0
- yiisoft/yii2: *
README
此扩展为Yii框架2.0添加购物车功能
安装
安装此扩展的最佳方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist omnilight/yii2-shopping-cart "*"
或
"omnilight/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])); } //... }
使用折扣
折扣作为附加到购物车或其位置的 behaviors 实现。要使用它们,请按照以下步骤操作
- 将折扣类定义为 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']);
注意,相同的 behavior 可以用于购物车和位置类。
- 获取应用折扣后的总价
$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; });