迪萨特科技/laravel-cart

此包的最新版本(dev-master)没有提供许可证信息。

购物车包

dev-master 2024-02-25 16:02 UTC

This package is auto-updated.

Last update: 2024-09-25 17:15:44 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形式返回,这将包括任何商品税费。如果您想获取不带税的购物车总额,可以将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();