basketin / cart
电子商务店铺的购物车
v1.0.0-beta10
2024-09-03 07:02 UTC
Requires
- php: ^8.1||^8.2
- filament/filament: ^3.2
- laravel/framework: ^10.0||^11.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-22 11:11:45 UTC
README
Basketin 购物车
Laravel 基础的电子商务系统购物车模块。
文档
安装
使用 composer 安装。
composer require basketin/cart
您需要迁移包表。
php artisan migrate --path=/vendor/basketin/cart/database/migrations
如果您需要在未设置路径的情况下进行授权迁移,可以在配置中将 basketin.cart.setup.auto_migrate
设置为 true
。
发布配置
php artisan vendor:publish --tag=basketin-cart-config
如何使用
创建新的购物车
<?php use Basketin\Component\Cart\Facades\CartManagement; $cart = CartManagement::initCart('01HF7V7N1MG9SDFPQYWXDNHR9Q', 'USD'); // <- ULID
如果购物车存在,您可以打开它,如果不存在,则创建一个新购物车。
打开现有购物车
<?php use Basketin\Component\Cart\Facades\CartManagement; $cart = CartManagement::openCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID
获取 Ulid
$cart->getUlid();
获取货币
$cart->getCurrency();
获取产品数量
$cart->getCountProducts();
获取商品数量
$cart->getCountItems();
仅打开现有购物车
添加引用。您需要准备一个 Product
模型来使用它。
// Product model <?php ... use Basketin\Component\Cart\Contracts\IQuote; use Basketin\Component\Cart\Traits\HasQuote; use Basketin\Component\Cart\Traits\HasTotal; class Product extends Model implements IQuote { use HasFactory; use HasQuote; use HasTotal; public function getOriginalPriceAttribute(): float { return (float) $this->price; } public function getSpecialPriceAttribute(): float|null { return null; } }
添加引用
<?php use App\Models\Product; use Basketin\Component\Cart\Facades\CartManagement; $product = Product::first(); $cart = CartManagement::openCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID $cart->quote()->addQuote($product, 1);
增加引用
<?php use App\Models\Product; use Basketin\Component\Cart\Facades\CartManagement; $product = Product::first(); $cart = CartManagement::openCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID $cart->quote()->increaseQuote($product, 5);
减少引用
<?php use App\Models\Product; use Basketin\Component\Cart\Facades\CartManagement; $product = Product::first(); $cart = CartManagement::openCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID $cart->quote()->decreaseQuote($product, 2);
有引用
<?php use App\Models\Product; use Basketin\Component\Cart\Facades\CartManagement; $product = Product::first(); $cart = CartManagement::openCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID $cart->quote()->hasQuote($product);
删除引用
<?php use App\Models\Product; use Basketin\Component\Cart\Facades\CartManagement; $product = Product::first(); $cart = CartManagement::openCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID $cart->quote()->removeQuote($product);
获取购物车
<?php use Basketin\Component\Cart\Facades\CartManagement; $cart = CartManagement::initCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID $cart->getCart();
获取引用
<?php use Basketin\Component\Cart\Facades\CartManagement; $cart = CartManagement::initCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID $cart->quote()->getQuotes();
获取总计
<?php use Basketin\Component\Cart\Facades\CartManagement; $cart = CartManagement::initCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID $totals = $cart->totals(); $totals->getSubTotal(); $totals->getDiscountTotal(); $totals->getGrandTotal();
如果您需要向购物车添加全局折扣,可以使用它。
$totals->setGlobalDiscountTotal(500.00) ->getGrandTotal();
优惠券
优惠券模型
您需要准备一个优惠券模型以注入到购物车服务中
use Illuminate\Database\Eloquent\Model; use Basketin\Component\Cart\Contracts\ICoupon; class Coupon extends Model implements ICoupon { protected $fillable = [ 'coupon_name', 'coupon_code', 'discount_type', 'discount_value', 'start_at', 'ends_at', ]; public function discountType(): String { return $this->discount_type; } public function discountValue(): Int { return $this->discount_value; } }
折扣类型:
fixed
=CouponCalculate::FIXED
|percent
=CouponCalculate::PERCENT
在购物车上应用优惠券代码:
$coupon = Coupon::first(); $cart->coupon($coupon);
您可以使用
$this->couponInfo()
获取优惠券信息
字段
您可以创建包含每个购物车键和值的字段。
设置字段
return $cart->fields()->set('key', 'value');
获取字段
return $cart->fields()->get('key');
删除
return $cart->fields()->remove('key');
有字段
return $cart->fields()->has('key');
贡献
感谢您考虑为这个包做出贡献!成为 Store 团队的一员。
许可
此包是开源软件,许可协议为 MIT 许可证。