darkflamed/moltin-cart

Moltin Cart包用于Laravel 5.4

v5.4.1 2017-04-29 22:11 UTC

This package is auto-updated.

Last update: 2024-09-12 04:23:19 UTC


README

Moltin\Cart的Laravel Facade和服务提供者

安装

通过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
);

设置商品的税率

你可以传递给insert方法的另一个键是'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();