osaris-uk / cart
使用数据库而不是会话来存储购物车的 Laravel 6 购物车库
Requires
- php: ^7.2 | ^8.0
- illuminate/support: >=6.0
Requires (Dev)
- fzaninotto/faker: ^1.4
- mockery/mockery: ^1.0
- orchestra/testbench: ^4.0
This package is auto-updated.
Last update: 2024-09-17 23:18:07 UTC
README
购物车
使用数据库而不是会话来存储购物车的 Laravel 6 购物车库。
特性
- 访客用户购物车
- 登录用户购物车
- 登录后,访客购物车与用户购物车合并
- 单例购物车实例以避免不必要的数据库查询。但也可以根据需要避免单例购物车。
- 基于 Eloquent 模型构建,易于扩展,并可以使用所有 Eloquent 方法。
- 多个购物车实例
- 计划过期购物车以进行删除
安装
-
使用 composer 命令安装
composer require osaris-uk/cart
-
接下来,在 config/app.php 中的 providers 数组中添加一个新的提供者
'providers' => [ //... OsarisUk\Cart\CartServiceProvider::class, //... ],
-
然后,发布数据库迁移并运行迁移
php artisan vendor:publish --provider="OsarisUk\Cart\CartServiceProvider" --tag=migrations php artisan migrate
配置
可选,您可以发布包配置文件
php artisan vendor:publish --provider="OsarisUk\Cart\CartServiceProvider" --tag=config
现在,如果需要,请更新 config/cart.php
用法
获取购物车实例
获取当前购物车实例。它返回一个单例购物车实例
$cart = app('cart'); //using app() helper
或者,
$cart = App::make('cart');
另外,您可以选择避免单例实例,并使用模型类在每次从数据库中加载购物车实例时使用
use OsarisUk\Cart\Models\Cart; //... $cart = Cart::current();
使用单例购物车的想法是,购物车对象将在您的应用全局范围内(例如控制器、模型、视图、视图组合器等)在整个请求中可用。另外,当您操作购物车项目时,$cart->item_count
和 $cart->total_price
将会更新。
添加项目: $cart->addItem($attributes)
$cart->addItem([ 'product_id' => 1, 'unit_price' => 10.5, 'quantity' => 1 ]);
相当于 $cart->items()->create($attributes)
获取项目: $cart->items
由于 $cart
是 Eloquent 模型实例,您可以使用任何 Eloquent 方法来获取项目
$items = $cart->items // by dynamic property access $items = $cart->items->get() $items = $cart->items->where('quantity', '>=', 2)->get()
更新项目: $cart->updateItem($where, $attributes)
$cart->updateItem([ 'id' => 2 ], [ 'product_id' => 1, 'unit_price' => 10.5, 'quantity' => 1 ]);
相当于 $cart->items()->where($where)->first()->update($attributes)
移除项目: $cart->removeItem($where)
$cart->removeItem([ 'id' => 2 ]);
相当于 $cart->items()->where($where)->first()->delete()
清除购物车项目: $cart->clear()
从购物车中移除所有项目
$cart->clear();
刷新购物车总计: $cart->refreshCartTotals()
根据购物车中的所有项目刷新购物车
$cart->refreshCartTotals();
结账购物车: $cart->checkout()
此方法仅更新 status
和 placed_at
列值。 status
设置为 pending
$cart->checkout();
在购物车之间移动项目
将一个购物车中的所有项目移动到另一个购物车实例中
$cart = app('cart'); $wishlist = app('cart', ['name' => 'wishlist']); //move all wishlist items to cart $wishlist->moveItemsTo($cart);
将单个项目在购物车之间移动
$cart = app('cart'); $wishlist = app('cart', ['name' => 'wishlist']); //move an wishlist item to cart $item = $wishlist->items()->where(['product_id' => 1])->first(); $item->moveTo($cart);
获取购物车属性
$total_price = $cart->total_price; // cart total price $item_count = $cart->item_count; // cart items count $date_placed = $cart->placed_at; // returns Carbon instance
购物车状态
支持多个购物车状态
active
:当前正在向购物车添加项目expired
:购物车已过期,对会话购物车有意义pending
:已结账的购物车completed
:完成的购物车
use OsarisUk\Cart\Models\Cart; // get carts based on their status: active/expired/pending/complete $active_carts = Cart::active()->get(); $expired_carts = Cart::expired()->get(); $pending_carts = Cart::pending()->get(); $completed_carts = Cart::completed()->get();
与多个购物车实例一起工作
默认情况下,购物车实例命名为 default
。您可以通过提供名称来加载其他实例
$cart = app('cart'); // default cart, same as: app('cart', [ 'name' => 'default']; $sales_cart = app('cart', [ 'name' => 'sales']); $wishlist = app('cart', [ 'name' => 'wishlist']);
或,不使用单例购物车
use OsarisUk\Cart\Models\Cart; //... $cart = Cart::current(); $sales_cart = Cart::current('sales'); $wishlist = Cart::current('wishlist');
要获取除 default
之外的其他购物车
$pending_sales_carts = Cart::instance('sales')->pending()->get();
删除过期购物车
访客购物车依赖于会话寿命。只要会话未过期,它们就是有效的。您可以在 config/session.php 中增加会话寿命以增加购物车寿命。当会话过期时,将在数据库中创建一个新的购物车实例,而旧的实例将不再被使用。随着时间的推移,这些过期购物车可能会在数据库中累积。
laravel任务调度器将提供帮助。要启用调度器,只需将以下内容添加到服务器上的crontab文件中
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
就这样。模块现在将每小时检查一次过期的购物车并删除它们。这不会影响已登录用户的购物车。
其他功能
获取购物车用户:$cart->user
获取商品项:$item->product
检查购物车是否为空:$cart->isEmpty()
如果购物车中存在商品项:$cart->hasItem(['id' => 10])
使购物车过期:cart->expire();
设置为完成
状态:$cart->complete();
扩展购物车模型
扩展购物车很容易。你可以扩展基本购物车模型并添加自己的方法或列。按照以下步骤扩展购物车模型
-
通过扩展
OsarisUk\Cart\Models\Cart
创建一个模型namespace App; use OsarisUk\Cart\Models\Cart as BaseCart; class Cart extends BaseCart { //override or add your methods here ... public function getSubTotalAttribute(){ return $this->attributes['total_price']; } public function getGrandTotalAttribute(){ //taking discount, tax etc. into account return $this->sub_total - $this->discount; } }
-
在
config/cart.php
中更新cart_model
为扩展模型的完整类名。'cart_model' => App\Cart::class,
-
就这样,你现在可以像平常一样加载购物车了
$cart = App::make('cart');
你还可以按照上述步骤创建自己的CartLine
模型,通过扩展OsarisUk\Cart\Models\CartLine
。请确保更新config/cart.php
以反映你的更改。
致谢
此包是hassansin/dbcart的修改版本。此包的许可证与原始版本保持不变。