yiddishe-kop / laravel-commerce
Laravel 的简单商业包装
Requires
- php: ^8.0
- illuminate/support: ^8.0|^9.0|^10.0
Requires (Dev)
- nunomaduro/collision: ^5.0|^6.1
- orchestra/testbench: ^7.7
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.2
- phpunit/phpunit: ^9.3.10
This package is auto-updated.
Last update: 2024-09-20 20:11:04 UTC
README
为 Laravel 提供的简单商业包装
在寻找 Laravel 的简单电子商务包装但没有找到轻量级且易于使用的解决方案后,我决定尝试自己创建一个。
在此处阅读官方文档: https://laravel-commerce.yiddishe-kop.com/
功能
- 购物车(存储在会话中 - 因此访客也可以有购物车)
- 订单
- 优惠券
- 特别优惠
- 多种货币
- 多种支付网关
此包装仅实现后端逻辑,并让您完全控制前端。
安装
您可以通过 composer 安装此包装
composer require yiddishe-kop/laravel-commerce
发布 commerce.php 配置文件
php artisan vendor:publish --provider="YiddisheKop\LaravelCommerce\CommerceServiceProvider" --tag="config"
如果需要自定义它们,您还可以发布迁移
php artisan vendor:publish --provider="YiddisheKop\LaravelCommerce\CommerceServiceProvider" --tag="migrations"
用法
购物车
您可以使用外观在任何地方访问购物车,无论用户是否已登录或为访客
use YiddisheKop\LaravelCommerce\Facades\Cart; $cart = Cart::get();
当访客登录时,购物车将附加到他的账户 👌。
注意:如果您希望购物车在登出后仍然可用,您需要覆盖 Auth\LoginController 中的以下方法
public function logout(Request $request) { $this->guard()->logout(); // keep cart data for after logout $cartId = session()->get('cart'); $request->session()->invalidate(); $request->session()->regenerateToken(); session()->put('cart', $cartId); if ($response = $this->loggedOut($request)) { return $response; } return $request->wantsJson() ? new JsonResponse([], 204) : redirect('/'); }
产品
您可以通过实现 Purchasable 合约使任何模型可购买
use YiddisheKop\LaravelCommerce\Contracts\Purchasable; use YiddisheKop\LaravelCommerce\Traits\Purchasable as PurchasableTrait; class Product implements Purchasable { use PurchasableTrait; // the title of the product public function getTitle(): string { return $this->name; } // the price public function getPrice(): int { return $this->price; } }
将产品添加到购物车
将产品添加到购物车非常简单
Cart::add(Purchasable $product, int $quantity = 1);
或者
$product->addToCart($quantity = 1);
如果您添加的产品已经存在于购物车中,我们将自动更新数量 😎。
从购物车中删除产品
Cart::remove(Purchasable $product);
或者
$product->removeFromCart();
清空整个购物车
Cart::empty();
访问购物车项目
您可以使用 items 关系访问购物车项目
$cartItems = $cart->items;
要从购物车项访问产品模型,请使用 model 关系(可变形态)
$product = $cart->items[0]->model;
计算总计
要计算和持久化购物车的总计,请使用 calculateTotals() 方法
Cart::calculateTotals();
现在购物车有以下最新数据
[
"items_total" => 3552
"tax_total" => 710.0
"coupon_total" => "0"
"grand_total" => 4262.0
]
在计算总计时,将自动从购物车中删除已删除的产品。
订单
您可以在用户模型上使用 HasOrders 特性,以获取 orders 关系
use YiddisheKop\LaravelCommerce\Traits\HasOrders; class User { use HasOrders; // ... } // you can now get all the users' orders (status complete) $orders = $user->orders;
测试
此包装具有广泛的测试 - 使用令人愉快的 Pest 框架。要运行测试
composer test
更改日志
请参阅 CHANGELOG 了解最近更改的更多信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全
如果您发现任何安全相关的问题,请通过电子邮件 yehuda@yiddishe-kop.com 而不是使用问题跟踪器。
鸣谢
许可
MIT 许可证(MIT)。请参阅 许可文件 了解更多信息。