moltin / laravel-cart
购物车包
Requires
- php: >=5.3.0
- laravel/framework: ~5.0
- moltin/cart: dev-master
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-14 14:16:12 UTC
README
本仓库正在考虑所有权转移,请参阅此处: #62 (评论)
Laravel 购物车包
Laravel Facade 和 Service Provider for Moltin\Cart
安装
通过 Composer
$ composer require moltin/laravel-cart
将以下内容添加到您的 app/config/app.php 文件的 service providers 数组中
'Moltin\Cart\CartServiceProvider',
然后添加到别名数组以下内容
'Cart' => 'Moltin\Cart\Facade',
然后您应该可以正常使用并可以通过以下静态接口访问购物车
// Format array of required info for item to be added to basket... $items = array( 'id' => 1, 'name' => 'Juicy Picnic Hamper', 'price' => 120.00, 'quantity' => 1 ); // Make the insert... Cart::insert($items); // Let's see what we have have in there... dd(Cart::totalItems());
###配置设置(可选)使用 php artisan vendor:publish
发布配置文件
return array( 'storage' => 'session', //session, cache, file // Cache // Laravel documentation for more information 'cache_prefix' => 'moltin_cart_', 'cache_expire' => '-1', //in minutes, -1 permanent caching // Filesystem // Folder Name inside the Storage Path 'storage_folder_name' => 'moltin_cart', // Identifier // With a requestcookie (choose for storage: cache, the session will be expired), the cart could be reloaded from a http request, example: the customer could save his cart link (email, hyperlink) and reopen this later. // If there is no request, the cookie will be loaded. 'identifier' => 'cookie', //cookie, requestcookie //Request Cookie 'requestid' => 'CartID', //http input request identifier, example: your customer/backoffice could reload the cart in your shop controller, /public/shop?CartID=871f0bc18ca76e68bf7c3adf8f9426ef );
设置商品税率
您还可以将 'tax' 作为另一个键传递给您的插入方法。这是一个百分比,您希望将其添加到商品价格上。
在以下示例中,我们将使用 20% 作为税率。
Cart::insert(array( 'id' => 'foo', 'name' => 'bar', 'price' => 100, 'quantity' => 1, 'tax' => 20 ));
更新购物车中的商品
您可以通过更新购物车项的任何属性来更新您的购物车中的商品。例如,如果您在一个购物车循环中,则可以使用以下示例更新一个特定商品。
foreach (Cart::contents() as $item) { $item->name = 'Foo'; $item->quantity = 1; }
删除购物车商品
您可以使用任何购物车项上的 remove()
方法来删除您的购物车中的任何商品。
foreach (Cart::contents() as $item) { $item->remove(); }
销毁/清空购物车
您可以使用 destroy()
方法完全清空/销毁购物车。
Cart::destroy()
检索购物车内容
您可以使用以下方法循环购物车内容
Cart::contents();
您还可以通过将 true 作为第一个参数传递来将购物车项作为数组返回
Cart::contents(true);
检索购物车中的商品总数
Cart::totalItems();
默认情况下,此方法将返回购物车中的所有商品及其数量。您可以将 true
作为第一个参数传递以获取所有唯一商品。
Cart::totalItems(true);
检索购物车总额
$cart->total();
默认情况下,total()
方法将返回购物车的总价值作为 float
,这将包括任何商品税。如果您想检索不含税的购物车总额,则可以在 total()
方法中传递 false
Cart::total(false);
检查购物车中是否有商品
Cart::has($itemIdentifier);
通过标识符检索商品对象
Cart::item($itemIdentifier);
购物车商品
购物车商品具有一些可能有助于集成购物车的功能。
检索商品的总额值
您可以使用以下方法检索特定购物车商品的总额值(包括数量)。
$item->total();
默认情况下,此方法将返回商品的总额加税。因此,如果您有一个价值 100 的产品,数量为 2,税率为 20%,则此方法返回的总计为 240。
您还可以通过将 false
传递给 total()
方法来获取减去税额的总额。
$item->total(false);
这将返回 200。
检查商品是否有选项
您可以使用 hasOptions()
方法检查购物车商品是否有选项。
if ($item->hasOptions()) { // We have options }
从购物车中删除商品
$item->remove();
以数组形式输出商品数据
$item->toArray();