omnilight/yii2-shopping-cart

此包的最新版本(1.3.0)没有可用的许可证信息。

为 Yii2 框架添加购物车功能的扩展

安装次数: 94,992

依赖项: 8

建议者: 0

安全性: 0

星标: 223

关注者: 37

分支: 112

开放问题: 3

类型:yii2-extension

1.3.0 2018-06-12 14:23 UTC

This package is auto-updated.

Last update: 2024-09-16 04:17:41 UTC


README

此扩展为 Yii 框架 2.0 添加购物车功能

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require --prefer-dist omnilight/yii2-shopping-cart "*"

或添加以下内容到你的 composer.json 的 require 部分。

"omnilight/yii2-shopping-cart": "*"

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 = Yii::$app->cart;

    $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]));
    }

    //...
}

使用折扣

折扣作为可以附加到购物车或其位置的的行为来实现。要使用它们,请按照以下步骤操作

  1. 将折扣类定义为 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;
    }
}
  1. 将此行为添加到购物车
$cart->attachBehavior('myDiscount', ['class' => 'app\components\MyDiscount']);

如果折扣不适用于整个购物车,而适用于个别位置,则可以将折扣附加到购物车位置本身

$cart->getPositionById($positionId)->attachBehavior('myDiscount', ['class' => 'app\components\MyDiscount']);

注意,同一行为可以用于购物车和位置类。

  1. 获取应用折扣后的总成本
$total = \Yii::$app->cart->getCost(true);
  1. 在计算过程中会触发以下事件
  • ShoppingCart::EVENT_COST_CALCULATION 每次计算时触发一次。
  • CartPositionInterface::EVENT_COST_CALCULATION 对购物车中的每个位置触发一次。

您也可以订阅这些事件以执行折扣计算

$cart->on(ShoppingCart::EVENT_COST_CALCULATION, function ($event) {
    $event->discountValue = 100;
});