?? webman 購物車插件 (session ,database)

簡介
購物車在電商場景中基本是必須的一個模塊
安裝
composer require yzh52521/webman-shop-cart
使用
選擇 Storage
您可以更改數(shù)據(jù)存儲在 config/plugin/yzh52521/shop-cart/app.php 配置文件
session
'storage' => \yzh52521\ShopCart\storage\SessionStorage::class,
datadate
'storage' => \yzh52521\ShopCart\storage\DatabaseStorage::class, //tp-orm
or
'storage' => \yzh52521\ShopCart\storage\LaravelDatabaseStorage::class, // laravel
如果更改數(shù)據(jù)存儲如果使用數(shù)據(jù)庫存儲,則需要創(chuàng)建數(shù)據(jù)表:
CREATE TABLE `shopping_cart`
(
`key` varchar(255) CHARACTER SET utf8 NOT NULL,
`__raw_id` varchar(255) CHARACTER SET utf8 NOT NULL,
`guard` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`user_id` int DEFAULT NULL,
`id` int NOT NULL,
`name` varchar(255) CHARACTER SET utf8 NOT NULL,
`qty` int NOT NULL,
`price` decimal(8, 2) NOT NULL,
`total` decimal(8, 2) NOT NULL,
`__model` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`type` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`status` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`attributes` text CHARACTER SET utf8,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`key`, `__raw_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
添加到購物車
add item
Item | null ShopCart::add(
string | int $id,
string $name,
int $quantity,
int | float $price
[, array $attributes = []]
);
example:
$row = ShopCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
// Item:
// id => 37
// name => 'Item name'
// qty => 5
// price => 100.00
// color => 'red'
// size => 'M'
// total => 500.00
// __raw_id => '8a48aa7c8e5202841ddaf767bb4d10da'
$rawId = $row->rawId();// get __raw_id
$row->qty; // 5
...
更新購物車
Update the specified item。
Item ShopCart::update(string $rawId, int $quantity);
Item ShopCart::update(string $rawId, array $arrtibutes);
example:
ShopCart::update('8a48aa7c8e5202841ddaf767bb4d10da', ['name' => 'New item name');
// or only update quantity
ShopCart::update('8a48aa7c8e5202841ddaf767bb4d10da', 5);
獲取購物車中所有商品
Get all the items.
Collection ShopCart::all();
example:
$items = ShopCart::all();
獲取一個商品
Get the specified item.
Item ShopCart::get(string $rawId);
example:
$item = ShopCart::get('8a48aa7c8e5202841ddaf767bb4d10da');
刪除商品
Remove the specified item by raw ID.
bool ShopCart::remove(string $rawId);
example:
ShopCart::remove('8a48aa7c8e5202841ddaf767bb4d10da');
清理購物車
Clean Shopping Cart.
bool ShopCart::destroy();
bool ShopCart::clean(); // alias of destroy();
example:
ShopCart::destroy();// or Cart::clean();
購物車總價格
Returns the total of all items.
int | float ShopCart::total(); // alias of totalPrice();
int | float ShopCart::totalPrice();
example:
$total = ShopCart::total();
// or
$total = ShopCart::totalPrice();
購物車商品個數(shù)
Return the number of rows
.
int ShopCart::countRows();
example:
ShopCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
ShopCart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']);
ShopCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
ShopCart::add(127, 'foobar', 15, 100.00, ['color' => 'green', 'size' => 'S']);
$rows = ShopCart::countRows(); // 2
購物車商品數(shù)量
Returns the quantity of all items
int ShopCart::count($totalItems = true);
$totalItems
: When false
,will return the number of rows.
example:
ShopCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
ShopCart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']);
ShopCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
$count = ShopCart::count(); // 11 (5+1+5)
搜索商品
Search items by property.
Collection ShopCart::search(array $conditions);
example:
$items = ShopCart::search(['color' => 'red']);
$items = ShopCart::search(['name' => 'Item name']);
$items = ShopCart::search(['qty' => 10]);
檢查購物車是否為空
bool ShopCart::isEmpty();
指定關(guān)聯(lián)的商品模型
Specifies the associated model of item.
ShopCart::associate(string $modelName);
example:
session
ShopCart::associate('app\model\Goods');
$item = ShopCart::get('8a48aa7c8e5202841ddaf767bb4d10da');
$item->goods->name; // $item->goods is instanceof 'app\model\Goods'
database/cache
ShopCart::associate('app\model\Goods');
ShopCart::name('web.1'); //The cart name like cart.{guard}.{user_id}: cart.api.1
$item = ShopCart::get('8a48aa7c8e5202841ddaf767bb4d10da');
$item->goods->name; // $item->goods is instanceof 'app\model\Goods'
購物車商品
properties of yzh52521\ShopCart\Item
:
id
- 商品IDname
- 商品名稱qty
- 商品數(shù)量price
- 商品單價total
- 商品總價.__raw_id
- 唯一ID.__model
- 模型關(guān)聯(lián)的名稱.- ...自定義屬性.
方法:
rawId()
- Return the raw ID of item.
事件
事件名 | 參數(shù) |
---|---|
cart.adding |
($attributes, $cart); |
cart.added |
($attributes, $cart); |
cart.updating |
($row, $cart); |
cart.updated |
($row, $cart); |
cart.removing |
($row, $cart); |
cart.removed |
($row, $cart); |
cart.destroying |
($cart); |
cart.destroyed |
($cart); |
您可以輕松處理這些事件,例如:
Event::listen('cart.adding', function($attributes, $cart){
// code
});
事件使用具體請看 http://www.wtbis.cn/plugin/27
database 必須設(shè)置購物車name
//The cart name likecart.{guard}.{user_id}
: cart.api.1
ShopCart::name('api.1');