ivankovrlija/laravel-cart

该包的最新版本(v5.0.1)没有提供许可证信息。

购物车包

v5.0.1 2015-10-19 21:13 UTC

This package is auto-updated.

Last update: 2024-09-28 01:22:58 UTC


README

此仓库正在考虑所有权转让,请参阅此处:moltin#62 (评论)

Laravel购物车包

Laravel Facade和Service Provider for Moltin\Cart

安装

通过Composer

$ composer require moltin/laravel-cart

将以下内容添加到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());

###配置设置(可选)使用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();