gauravojha / shoppingcart-eloquent
一个简单的基于Laravel的购物车。使用持久的Eloquent数据库而不是会话。
这个包的规范存储库似乎已经消失了,因此该包已被冻结。
Requires
- php: >=5.5.9
- illuminate/database: ^5.2
- illuminate/support: ^5.2
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2021-07-02 05:47:17 UTC
README
一个简单的基于Laravel的购物车。使用持久的Eloquent数据库而不是会话。
注意
如果您有时间,我请求您阅读动机,了解我最初选择制作此包的原因,以及它与其他Laravel基于的包有何不同。当然,您也可以直接开始使用!
另一件事,我知道测试用例一团糟。我打算尽快修复这个问题。
安装
您可以通过 Composer 安装此包。
从终端运行Composer require命令
composer require gauravojha/shoppingcart-eloquent
接下来,添加包的服务提供程序并别名包。打开 config/app.php
文件,
-
将以下行添加到
providers
数组Gojha\Shoppingcart\ShoppingCartProvider::class,
-
将以下行添加到
aliases
数组'Cart' => Gojha\Shoppingcart\CartFacade::class,
由于此包使用Eloquent数据库和相关的模型,您还需要从应用程序的根目录运行以下命令
> php artisan vendor:publish --provider="Gojha\Shoppingcart\ShoppingCartProvider"
当然,无需多言,运行迁移以创建数据库,使用 php artisan migrate
完成以上步骤后,您就可以开始使用此购物车了。
用法
以下所有使用场景都在示例部分使用单个控制器进行说明
以下方法使用模型 App\ShoppingCartItem
,它作为 vendor:publish
命令的一部分自动提供给您。
购物车提供了以下方法供您使用
Cart::addToCart($productid, $price, $quantity)
将指定的数量或 $quantity
的通过产品(通过其唯一的 $productid
标识)添加到购物车中。在此实现中,$price
是单个商品的价格,而不是购物车中所有类似商品的总成本。
// get a product to add to the cart // this will fetch from the database a product, which has 'id' equal to $id $product = Product::find($id); // now add 4 such products to the cart Cart::addToCart($product->id, $product->price, 4);
如果用户已经将相同的产品添加到购物车中,则会更新数量,而不是创建新的记录。
Cart::content()
将返回所有购物车内容给用户。一旦您有了内容,您可以从其中检索 'product_id',然后根据该id从数据库中检索产品并对其进行操作。
// obtain all the products in the cart $contents = Cart::content(); // obtain the no.of products in the cart $count = Cart::content()->count();
Cart::removeFromCart($productid)
将删除购物车中 id
等于传递的 $productid
的产品。
// if a product exists in the cart which has a $product->id = 4, then it will be removed from the cart in its entirety Cart::removeFromCart(4);
Cart::emptyCart()
销毁购物车中的所有内容。
// remove all the products in the shopping cart Cart::emptyCart();
Cart::updateItemInCart($productid, $quantity)
更新购物车中产品 $quantity
的数量,其中产品->id 等于 $productid
。
// change the quantity of products. // Assuming the cart has a product which has a product with id '4', and you want to change its quantity to 1. Cart::updateItemInCart(4, 1);
Cart::totalPrice()
每个应用可能都有不同的计算购物车中商品总成本的过程。在本实现中,在ShoppingCartImpl
文件内部,我声明了两个私有变量$taxRate
和$deliveryCharges
,并设置了默认值。方法totalPrice
通过首先加上购物车中所有商品的总成本,然后加上税费和运费,来计算客户结账时需要支付的总价。代码非常直接和简单,因此应该不会很难修改它并按照您的要求使用它。
$totalPrice = Cart::totalPrice();
这就是全部内容。由于记录是使用经过身份验证的用户ID插入的,所以不会发生冲突,其他用户也无法查看您的记录。此外,您可以添加产品,去为期一周的假期,由于数据是持久化的,您可以找到您离开的地方的产品。
示例
如前所述,此包(如果您按原样使用),对您施加了一些基本约束/假设。
对于本节
- 假设有一个简单的表'products',具有以下结构
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateProductsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->string('product_name')->unique(); $table->decimal('price_per_item')->default(0.00); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('products'); } }
- 假设有一个具有以下框架的控制器
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Product; use Cart; use Auth; class CartController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct(Request $request) { $this->middleware('auth'); } ... // Remaining controller code here ... }
现在,如果您有一个用于将产品添加到购物车的route
,那么一个示例将是
public function addToCart(Request $request, Product $product) { // the request must supply the product to be added and other relevant data Cart::addToCart(Auth::user()->id, $product->id, $product->price_per_item, $request->input('quantity')); }
我希望这个例子足以让您开始并探索
即将推出的功能
我明白这个包非常基础,可能只是满足用户的要求。此外,它不是非常健壮或动态(例如,肯定会有这样的场景,用户已经将一些产品添加到购物车,此时应用程序所有者同时提高了或降低了产品的价格)。因此,我打算接下来承担以下任务
- 如果产品数据库中的价格发生变化,更新
仍然在购物车中的
产品的价格。 - 引入Redis支持
- 修复测试用例
动机
在您继续之前,请注意,这与其他可用的购物车不同。话虽如此,我并不想贬低那些实现。其他的是非常健壮的,有很多选项。
然而,虽然我亲自尝试了其中大部分,但我从未完全满意。在我看来,一些缺点是
- laravel sessions的使用。虽然功能强大,但对我来说意味着
- 当会话结束时,购物车中的项目会消失。我作为客户,为什么要将一些产品添加到购物车,决定稍后支付,然后几小时后回来,却发现我的购物车空了!!!
- 一些购物车有缺陷,即购物车不安全。如果我以用户A的身份登录,添加了一些项目,然后注销。然后使用同一浏览器,我以用户B的身份登录。我仍然可以看到A添加的项目。
- 大多数购物车要求开发人员传递一个唯一的'行标识符'或使用购物车实例。还有一些我暂时想不起来的...
因此,我决定设计自己的购物车,它不使用会话。而是使用持久数据库来处理购物车。行是通过登录用户的'标识符'和要添加的产品'标识符'来识别的。再见,会话!
此购物车做了一些假设。所以只有当你清楚地了解它对您作为开发人员施加的限制时,才尝试使用它。如果您可以使用这个包,请
- 您将只允许已注册并登录的用户购买产品,因为这个包使用
Auth
服务提供者来插入用户标识。 - 您有一个产品表,其中每个产品都有一个唯一的id,可以通过它来识别。在代码中(参见提供的迁移),我使用了users表和我自己的products表。请随意使用您认为合适的表。此外,这是一个我自己使用的场景。请随意根据您的喜好修改代码并使用。
希望您觉得这个包很有用且易于使用。