nethask / laravel-cart
购物车包
Requires
- php: >=5.3.0
- laravel/framework: 4.*
- nethask/cart: 1.0.0
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-20 19:35:15 UTC
README
来自 (https://github.com/moltin/laravel-cart)[https://github.com/moltin/laravel-cart]
Moltin\Cart 的 Laravel Facade 和 Service Provider
安装
要使用,只需通过 Composer 安装此包,然后将以下内容添加到您的 app/config/app.php 中的服务提供者数组
'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());
### 配置设置(可选)在 app/config 文件夹中添加文件 moltincart.php
return array( 'storage' => 'session', //session, cache // Cache // Laravel documentation for more information 'cache_prefix' => 'moltin_cart_', 'cache_expire' => '-1', //in minutes, -1 permanent caching // 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
,这包括任何商品税。如果您想获取不含税的购物车总额,可以将 false
传递给 total()
方法
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();