devanych/yii2-cart

为Yii2的购物车

安装量: 7,264

依赖者: 0

建议者: 0

安全性: 0

星标: 20

关注者: 2

分支: 12

开放性问题: 0

类型:yii2-extension

v1.2.5 2019-10-18 10:25 UTC

This package is auto-updated.

Last update: 2024-09-13 20:35:11 UTC


README

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

在此提供了俄语的详细说明指南。

安装

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

运行

php composer.phar require devanych/yii2-cart "*"

或在您的composer.json文件的require部分中添加

devanych/yii2-cart: "*"

配置

配置cart组件(默认值如下所示)

return [
    //...
    'components' => [
        //...
        'cart' => [
            'class' => 'devanych\cart\Cart',
            'storageClass' => 'devanych\cart\storage\SessionStorage',
            'calculatorClass' => 'devanych\cart\calculators\SimpleCalculator',
            'params' => [
                'key' => 'cart',
                'expire' => 604800,
                'productClass' => 'app\model\Product',
                'productFieldId' => 'id',
                'productFieldPrice' => 'price',
            ],
        ],
    ]
    //...
];

除了devanych\cart\storage\SessionStorage之外,还有devanych\cart\storage\CookieStoragedevanych\cart\storage\DbSessionStorage。您还可以创建自己的存储,您需要实现接口devanych\cart\storage\StorageInterface

DbSessionStorage使用SessionStorage处理未授权用户,使用数据库处理授权用户。

如果您将devanych\cart\storage\DbSessionStorage作为storageClass使用,则需要应用以下迁移

php yii migrate --migrationPath=@vendor/devanych/yii2-cart/migrations

devanych\cart\calculators\SimpleCalculator生成购物车中商品的总成本和总数量。如果您需要进行带有折扣或其他计算的运算,可以创建自己的计算器,通过实现接口devanych\cart\calculators\CalculatorInterface

设置params数组

  • key - 对于会话和Cookie。

  • expire - Cookie的有效期。

  • productClass - 产品类是一个ActiveRecord模型。

  • productFieldId - 产品模型的id字段名称。

  • productFieldPrice - 产品模型的price字段名称。

支持同一网站上的多个购物车

//...
'cart' => [
    'class' => 'devanych\cart\Cart',
    'storageClass' => 'devanych\cart\storage\SessionStorage',
    'calculatorClass' => 'devanych\cart\calculators\SimpleCalculator',
    'params' => [
        'key' => 'cart',
        'expire' => 604800,
        'productClass' => 'app\model\Product',
        'productFieldId' => 'id',
        'productFieldPrice' => 'price',
    ],
],
'favorite' => [
    'class' => 'devanych\cart\Cart',
    'storageClass' => 'devanych\cart\storage\DbSessionStorage',
    'calculatorClass' => 'devanych\cart\calculators\SimpleCalculator',
    'params' => [
        'key' => 'favorite',
        'expire' => 604800,
        'productClass' => 'app\models\Product',
        'productFieldId' => 'id',
        'productFieldPrice' => 'price',
    ],
],
//...

用法

您可以在应用程序的任何地方使用Yii::$app->cart获取购物车组件。

使用购物车

// Product is an AR model
$product = Product::findOne(1);

// Get component of the cart
$cart = \Yii::$app->cart;

// Add an item to the cart
$cart->add($product, $quantity);

// Adding item quantity in the cart
$cart->plus($product->id, $quantity);

// Change item quantity in the cart
$cart->change($product->id, $quantity);

// Removes an items from the cart
$cart->remove($product->id);

// Removes all items from the cart
$cart->clear();

// Get all items from the cart
$cart->getItems();

// Get an item from the cart
$cart->getItem($product->id);

// Get ids array all items from the cart
$cart->getItemIds();

// Get total cost all items from the cart
$cart->getTotalCost();

// Get total count all items from the cart
$cart->getTotalCount();

使用购物车项

// Product is an AR model
$product = Product::findOne(1);

// Get component of the cart
$cart = \Yii::$app->cart;

// Get an item from the cart
$item = $cart->getItem($product->id);

// Get the id of the item
$item->getId();

// Get the price of the item
$item->getPrice();

// Get the product, AR model
$item->getProduct();

// Get the cost of the item
$item->getCost();

// Get the quantity of the item
$item->getQuantity();

// Set the quantity of the item
$item->setQuantity($quantity);

通过使用方法getProduct(),您可以访问产品的所有属性和方法。

$product = $item->getProduct();

echo $product->name;