tefo / cart
Requires
- illuminate/auth: 6.* || 8.*
- illuminate/contracts: 6.* || 8.*
- illuminate/events: 6.* || 8.*
- illuminate/session: 6.* || 8.*
- illuminate/support: 6.* || 8.*
Requires (Dev)
- mockery/mockery: ^1.4
- orchestra/testbench: ^6.13
- phpunit/phpunit: 8.* || 9.*
README
Laravel 的简单购物车实现。
安装
通过 Composer 安装此包。
从终端运行 Composer require 命令
composer require tefo/cart
现在你可以开始在你的应用程序中使用购物车了。
概览
查看以下主题以了解更多关于购物车的内容
用法
首先实现你想要添加到购物车的模型,需要实现 HasCart
接口
use Tefo\Cart\HasCart; use Tefo\Cart\InteractsWithCard; class Product extends Model implements HasCart { use InteractsWithCard; // ... }
现在你可以使用以下购物车方法了
Cart::addItem()
要向购物车添加一个项目,请使用此方法。第一个参数接受实现 HasCart
或数组的商品。
Cart::addItem($product);
此方法有可选参数,第二个参数可以设置商品选项,如数量。如果没有设置数量,默认为 1。第三个可选参数可以设置产品变体,如 ['color' => 'red']
。
Cart::addItem($product, ['quantity' => 2], ['color' => 'red']);
Cart::add()
添加多个商品到购物车非常简单,只需使用 add()
方法,该方法接受各种参数。
第一个参数接受商品信息的数组,每个商品可以是 eloquent 模型或数组。
Cart::add([$product_1, $product_2]);
第二个数组接受每个商品选项的数组,如产品数量,这是可选的。最后一个参数也是可选的,接受每个产品的变体数组。
Cart::add([$product_1, $product_2], [['quantity' => 1], ['quantity' => 2]], [$product_1_Variation, $product_2_Variation]);
该方法将返回一个包含刚刚添加到购物车的商品的 CartItem
实例集合。
Cart::update()
要更新购物车中的商品,你需要首先获取商品的 itemId。然后,你可以使用 update()
方法来更新它。
如果你想更新商品的更多属性,你可以将数组或 HasCart
作为第二个参数传递给更新方法。这样,你可以使用给定的 itemId 更新商品的所有信息。
$itemId = '92e1b081-17b9-4f7a-8f91-91da9dbcd6ca'; // its uuid Cart::update($itemId, $updatedProduct); // Will update the id, name and price Cart::update($itemId, ['name' => 'Product 1']); // Will update the name
如果你想简单地更新数量,请将 itemId 和新数量传递给更新方法
$itemId = '92e1b081-17b9-4f7a-8f91-91da9dbcd6ca'; Cart::updateQuantity($itemId, 2); // Will update the quantity
Cart::remove()
要从购物车中删除一个项目,你将再次需要 itemId。将此 itemId 简单地传递给 remove()
方法,它将从购物车中删除该商品。
$itemId = '92e1b081-17b9-4f7a-8f91-91da9dbcd6ca'; Cart::remove($itemId);
Cart::get()
如果你想要通过其 itemId 从购物车中获取一个商品,你可以在购物车上简单地调用 get()
方法,并传递 itemId。
$itemId = '92e1b081-17b9-4f7a-8f91-91da9dbcd6ca'; Cart::get($itemId);
Cart::content()
当然,你还需要获取购物车的商品内容。这就是你将使用 content
方法的场合。此方法将返回一个包含 CartItem
的集合,你可以遍历它并向客户显示内容。
Cart::content();
此方法将返回当前购物车实例的内容,如果你想获取另一个实例的内容,只需链式调用。
Cart::session('wishlist')->content();
Cart::destroy()
如果你想完全删除购物车的内容,可以在购物车上调用 destroy 方法。这将删除当前购物车实例的所有 CartItem
。
Cart::destroy();
Cart::total()
使用 total()
方法可以获取购物车中所有商品的计算总价,包括其价格和数量。
Cart::total();
此方法将自动格式化结果,你可以使用三个可选参数进行微调
Cart::total($decimals, $decimalSeperator, $thousandSeperator);
你可以在配置文件中设置默认的数字格式。
如果您没有使用外观模式,但在您的控制器(例如)中使用依赖注入,您也可以简单地获取总属性 $cart->total
Cart::tax()
使用 tax()
方法可以获取购物车中所有项目的计算税额,前提是给出了价格和数量。
Cart::tax();
此方法将自动格式化结果,你可以使用三个可选参数进行微调
Cart::tax($decimals, $decimalSeperator, $thousandSeperator);
你可以在配置文件中设置默认的数字格式。
如果您没有使用外观模式,但在您的控制器(例如)中使用依赖注入,您也可以简单地获取税属性 $cart->tax
Cart::subtotal()
subtotal()
方法可以用来获取购物车中所有项目的总额,减去税总额。
Cart::subtotal();
此方法将自动格式化结果,你可以使用三个可选参数进行微调
Cart::subtotal($decimals, $decimalSeperator, $thousandSeperator);
你可以在配置文件中设置默认的数字格式。
如果您没有使用外观模式,但在您的控制器(例如)中使用依赖注入,您也可以简单地获取小计属性 $cart->subtotal
Cart::count()
如果您想知道购物车中有多少个项目,您可以使用 count()
方法。此方法将返回购物车中的项目总数。所以如果您添加了2本书和1件衬衫,它将返回3个项目。
Cart::count();
Cart::search()
要查找购物车中的项目,您可以使用 search()
方法。
在幕后,该方法只是简单地使用了 Laravel Collection 类的 filter 方法。这意味着您必须传递一个闭包,在其中您将指定搜索条件。
例如,如果您想找到所有 ID 为 1 的项目
$cart->search(function ($cartItem, $itemId) { return $cartItem->id === 1; });
如您所见,闭包将接收两个参数。第一个是执行检查的 CartItem。第二个参数是此 CartItem 的 itemId。
此方法将返回一个包含找到的所有 CartItem 的 Collection。
这种搜索方式让您完全控制搜索过程,并能够创建非常精确和特定的搜索。
集合
在多个实例中,购物车将返回一个 Collection。这只是简单的 Laravel Collection,因此您可以在结果上调用 Laravel Collection 的所有方法。
例如,您可以快速获取购物车中独特产品的数量
Cart::content()->count();
或者按产品 ID 对内容进行分组
Cart::content()->groupBy('id');
实例
此包支持购物车的多个实例。其工作方式如下
您可以通过调用 Cart::session('newInstance')
来设置当前购物车实例。从此时起,活动的购物车实例将是 newInstance
,因此当您添加、删除或获取购物车的内时,您将处理 newInstance
的购物车实例。如果您想切换实例,只需再次调用 Cart::session('otherInstance')
,您将再次处理 otherInstance
。
所以这里有一个小例子
Cart::session('shopping')->addItem(['id' => 1, 'name' => 'Product 1', 'price' => 9.99], ['quantity' => 1]); // Get the content of the 'shopping' cart Cart::content(); Cart::session('wishlist')->addItem($product_2, ['quantity' => 2], ['size' => 'medium']); // Get the content of the 'wishlist' cart Cart::content(); // If you want to get the content of the 'shopping' cart again Cart::session('shopping')->content(); // And the count of the 'wishlist' cart again Cart::session('wishlist')->count();
注意。请记住,在脚本执行期间,如果没有设置不同的实例,购物车将保持最后一个设置的实例。
注意2。默认的购物车实例称为 default
,因此当您不使用实例时,Cart::content();
与 Cart::instance('default')->content()
相同。
模型
因为直接从 CartItem 访问模型非常方便,所以可以将模型与购物车中的项目相关联。假设您在应用程序中有一个 Product
模型。使用 associate()
方法,您可以告诉购物车购物车中的某个项目与 Product
模型相关联。
这样,您就可以直接从 CartItem
访问模型了!
您可以通过 CartItem
上的 model
属性访问模型。
如果您的模型实现了 HasCart
接口,并且您使用模型将项目添加到购物车,它将自动关联。
这里有一个例子
// First we'll add the item to the cart. $cartItem = Cart::addItem(['id' => 1, 'name' => 'Product 1', 'price' => 9.99], ['quantity' => 2], ['size' => 'large']); // Next we associate a model with the item. Cart::associate($cartItem->itemId, 'Product'); // Or even easier, call the associate method on the CartItem! $cartItem->associate('Product'); // You can even make it a one-liner Cart::addItem(['id' => 2, 'name' => 'Product 2', 'price' => 9.99], ['quantity' => 1], ['size' => 'large'])->associate('Product'); // Now, when iterating over the content of the cart, you can access the model. foreach(Cart::content() as $row) { echo 'You have ' . $row->quantity . ' items of ' . $row->model->name . ' with description: "' . $row->model->description . '" in your cart.'; }
配置
要发布 config
文件。
php artisan vendor:publish --provider="Tefo\Cart\CartServiceProvider" --tag="config"
这将为您提供一个 cart.php
配置文件,您可以在其中进行更改。
异常
购物车包在出现问题时会抛出异常。这样,您可以使用购物车包更容易地调试代码,或者根据异常类型处理错误。购物车包可以抛出以下异常
事件
购物车还内置了事件。有五个事件可供您监听。
示例
以下是如何在表格中列出购物车内容的简单示例
// Add some items in your Controller. Cart::addItem(['id' => 1, 'name' => 'Product 1', 'price' => 9.99], ['quantity' => 1]); Cart::addItem(['id' => 2, 'name' => 'Product 2', 'price' => 5.95], ['quantity' => 2], ['size' => 'large']); // Display the content in a View. <table> <thead> <tr> <th>Product</th> <th>Quantity</th> <th>Price</th> <th>Subtotal</th> </tr> </thead> <tbody> @foreach(Cart::content() as $row) <tr> <td> <p><strong>{{ $row->name }}</strong></p> <p>@if($row->variation->has('size')) {{ $row->variation->size }} @endif</p> </td> <td><input type="text" value="{{ $row->quantity }}"></td> <td>${{ $row->price }}</td> <td>${{ $row->total }}</td> </tr> @endforeach </tbody> <tfoot> <tr> <td colspan="2"> </td> <td>Subtotal</td> <td>{{ Cart::subtotal() }}</td> </tr> <tr> <td colspan="2"> </td> <td>Tax</td> <td>{{ Cart::tax() }}</td> </tr> <tr> <td colspan="2"> </td> <td>Total</td> <td>{{ Cart::total() }}</td> </tr> </tfoot> </table>