webkadabra/yii2-shopping-cart

此包的最新版本(dev-master)没有可用的许可信息。

Yii2扩展,用于添加购物车功能

安装: 10

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 112

类型:yii2-extension

dev-master 2016-04-15 14:50 UTC

This package is not auto-updated.

Last update: 2024-09-18 19:27:42 UTC


README

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

安装

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

运行以下命令之一:

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

或者将以下内容添加到你的composer.json文件的require部分:

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

使用方法

在你的模型中

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

    //...
}

使用折扣

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

  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. 将此behavior添加到购物车中
$cart->attachBehavior('myDiscount', ['class' => 'app\components\MyDiscount']);

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

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

注意,相同的behavior可以用于购物车和位置类。

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