melihovv / laravel-shopping-cart
此包已废弃,不再维护。未建议替代包。
laravel的购物车包
7.0.2
2020-10-05 20:23 UTC
Requires
- php: >=7.2
- ext-json: *
- illuminate/container: ^6.0|^7.0|^8.0
- illuminate/database: ^6.0|^7.0|^8.0
- illuminate/redis: ^6.0|^7.0|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ^4.0|^5.0|^6.0
- phpunit/phpunit: ^8.4|^9.0
- predis/predis: ^1.1
README
安装
通过composer安装
composer require melihovv/laravel-shopping-cart
发布配置文件和迁移
php artisan vendor:publish --provider="Melihovv\ShoppingCart\ServiceProvider"
运行迁移
php artisan migrate
概览
用法
在config/app.php中注册facade
'Cart' => 'Melihovv\ShoppingCart\Facades\ShoppingCart',
或
use Melihovv\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' => \Melihovv\ShoppingCart\Repositories\ShoppingCartDatabaseRepository::class, // or 'repository' => \Melihovv\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
安全
如果您发现任何与安全相关的问题,请通过电子邮件发送至 amelihovv@ya.ru,而不是使用问题跟踪器。