sky2002/laravel-shopping-cart

laravel的购物车包

8.2.2.1 2024-05-17 15:51 UTC

README

此购物车来源于 melihovv/laravel-shopping-cart。

因为 melihovv/laravel-shopping-cart 已存档。我复制了文件并进行了更新。

GitHub Workflow Status styleci

Packagist Packagist Packagist

安装

通过composer安装

composer require sky2002/laravel-shopping-cart

发布配置文件和迁移

php artisan vendor:publish --provider="sky2002\ShoppingCart\ServiceProvider"

运行迁移

php artisan migrate

概览

用法

在 config/app.php 中注册外观

'Cart' => 'sky2002\ShoppingCart\Facades\ShoppingCart',

use sky2002\ShoppingCart\Facades\ShoppingCart as Cart;

在以下示例中。

购物车提供了以下方法供您使用

Cart::add()

向购物车添加一个项目。

$cartItem = Cart::add($id, $name, $price, $quantity);
$cartItem = Cart::add($id, $name, $price, $quantity, [
    'color' => 'white',
]);

Cart::remove()

从购物车中移除具有指定唯一ID的项目。唯一ID用于存储具有相同 $id 但不同 $options 的项目。

$cartItem = Cart::add($id, $name, $price, $quantity);

// ...

Cart::remove($cartItem->getUniqueId())

Cart::has()

检查购物车是否包含具有指定唯一ID的项目。

$cartItem = Cart::add($id, $name, $price, $quantity);

// ...

if (Cart::has($cartItem->getUniqueId())) {
    // Do smth.
}

Cart::get()

通过唯一ID获取购物车中的项目。

$cartItem = Cart::add($id, $name, $price, $quantity);

// ...

$cartItem = Cart::get($cartItem->getUniqueId());

Cart::content()

获取购物车中的所有项目。

Cart::clear()

清空购物车。

Cart::count()

返回购物车中的项目数量。此方法不汇总项目数量。例如,购物车中有3本书和1个iPhone,此方法返回2。

Cart::getTotal()

返回购物车中所有项目的总价。

Cart::add(1, 'iPhone 7', 500, 1);
Cart::add(1, 'iPad Pro', 700, 2);
Cart::getTotal(); // return 1900

实例

该包支持购物车的多个实例。以下是一些示例

Cart::instance('shopping')->add('192ao12', 'Product 1', 100, 10);

// Store and get the content of the 'shopping' cart
Cart::store->content();

Cart::instance('wishlist')->add('sdjk922', 'Product 2', 50, 1, ['size' => 'medium']);

// Store and get the content of the 'wishlist' cart
Cart::store()->content();

// If you want to get the content of the 'shopping' cart again
Cart::instance('shopping')->restore()->content();

默认购物车实例称为 default,因此当您不使用实例时,Cart::content();Cart::instance('default')->content() 相同。

Cart::instance()

设置当前实例名称。

Cart::currentInstance()

获取当前实例名称。

存储

目前有两个可能的存储方式来持久化购物车

  • 数据库
  • Redis

您可以通过在配置中指定存储库类名称来选择其中之一

// config/shopping-cart.php

'repository' => \sky2002\ShoppingCart\Repositories\ShoppingCartDatabaseRepository::class,
// or
'repository' => \sky2002\ShoppingCart\Repositories\ShoppingCartRedisRepository::class,

为了使用Redis存储,您还需要安装 predis/predis 包。

Cart::store()

将当前购物车实例持久化到存储。

Cart::store($user->id);
Cart::instance('cart')->store($user->id);
Cart::instance('wishlist')->store($user->id);

Cart::restore()

将购物车实例恢复到存储。

Cart::restore($user->id);
Cart::instance('cart')->restore($user->id);
Cart::instance('wishlist')->restore($user->id);

Cart::destroy()

从存储中删除购物车实例。

Cart::destroy($user->id);
Cart::instance('cart')->destroy($user->id);
Cart::instance('wishlist')->destroy($user->id);

优惠券

您可以向购物车轻松添加折扣优惠券。目前有两种类型的优惠券

  • FixedDiscountCoupon
  • PercentDiscountCoupon

相关方法

Cart::addCoupon()

向购物车添加优惠券。

Cart::addCoupon(new FixedDiscountCoupon($name, $discount));
Cart::addCoupon(new PercentDiscountCoupon($name, 0.1)); // 10% discount

Cart::coupons()

返回所有优惠券。

Cart::getTotalWithCoupons()

返回应用优惠券后的总价。

Cart::add(1, 'iPhone 7', 500, 1);
Cart::add(1, 'iPad Pro', 700, 2);
Cart::addCoupon(new FixedDiscountCoupon($name, 300));
Cart::getTotal(); // return 1900 - 300 = 1600