yabhq/laravel-cart

简单易用且可定制的 Laravel 购物车

v0.20.1 2022-09-06 16:29 UTC

README

Latest Version on Packagist CircleCI

Laravel 购物车

这是一个简单易用且可定制的 Laravel 购物车实现。

提供开箱即用的 RESTful API 端点,以帮助前端/SPA 集成。

目录

要求
安装
使用 结账类
定制
许可证

要求

  • PHP 8+
  • Laravel 8.x

安装

composer require yabhq/laravel-cart

该包发布了一些迁移、路由(可选使用)和类,以进一步自定义您的商店物流。

php artisan vendor:publish --provider="Yab\ShoppingCart\ShoppingCartServiceProvider"

发布的文件完整列表

  • database/migrations/2020_12_13_000001_create_carts_table
  • database/migrations/2020_12_13_000002_create_cart_items_table
  • routes/checkout.php
  • config/checkout.php
  • app/Logistics/CartLogistics.php
  • app/Logistics/ShippingLogistics.php
  • app/Logistics/TaxLogistics.php
  • app/Logistics/DiscountLogistics.php

使用

首先,简单地在您的产品(或其他可购买)模型上实现 Purchaseable 接口。

app/Models/Product.php

use Yab\ShoppingCart\Traits\Purchaseable;
use Yab\ShoppingCart\Contracts\Purchaseable as PurchaseableInterface;

class Product extends Model implements PurchaseableInterface
{
    use Purchaseable;
}

接下来,我们应该在代表最终客户的模型上实现 Purchaser 接口。

app/Models/Customer.php

use Yab\ShoppingCart\Traits\Purchaser;
use Yab\ShoppingCart\Contracts\Purchaser as PurchaserInterface;

class Customer extends Model implements PurchaserInterface
{
    use Purchaser;
}

如果您想使用内置的购物车 API 端点,只需将发布的 checkout.php 包含到您现有的路由文件中。

routes/api.php (可选)

Route::group(['middleware' => ['example']], function () {
    require base_path('routes/checkout.php');
});

结账类

该包附带一个 Checkout 类,允许您与购物车交互。

use Yab\ShoppingCart\Checkout;

创建或检索结账实例

$checkout = Checkout::create();
// or
$checkout = Checkout::findById('uuid-123');

获取现有结账的 ID

$checkout->id();

为结账添加自定义字段

$checkout->setCustomField('some key', 'some value');

删除结账

$checkout->destroy();

与底层的购物车模型和查询构建器交互

// Yab\ShoppingCart\Models\Cart
$checkout->getCart();

// Illuminate\Database\Eloquent\Builder
$checkout->getCartBuilder();

添加、更新或删除购物车项目

// Add 1 qty of product and return the CartItem model
$item = $checkout->addItem($product, 1);

// Override the default unit price for the product
$item = $checkout->addItem($product, 1, 11.95);

// Add custom options to a checkout item
$item = $checkout->addItem(
    purchaseable: $product,
    qty: 1,
    options: [ 'size' => 'medium' ],
);

// Update the quantity of the item to 2
$checkout->updateItem($item->id, 2);

// Remove the item entirely
$checkout->removeItem($item->id);

可选设置购买实体(类必须实现 Purchaser 接口)

$checkout->setPurchaser($customer);

获取运费、小计、税费和总计

$checkout->getShipping(); // 5.00
$checkout->getSubtotal(); // 110.00
$checkout->getDiscount(); // 10.00
$checkout->getTaxes(); // 13.00
$checkout->getTotal(); // 113.00

定制

并非每个电子商务商店都是相同的。此包提供了一些“物流”类,允许您挂钩到核心包逻辑并执行一些常见自定义。例如,您可以指定如何确定税费、运费和折扣金额

app/Logistics/TaxLogistics.php

public static function getTaxes(Checkout $checkout) : float

app/Logistics/ShippingLogistics.php

public static function getShippingCost(Checkout $checkout) : float

app/Logistics/DiscountLogistics.php

public static function getDiscountFromCode(Checkout $checkout, string $code) : float

app/Logistics/CartLogistics.php

public static function getPurchaseable(string $type, mixed $id) : mixed
public static function beforeCartItemAdded(Checkout $checkout, mixed $purchaseable, int $qty) : void
public static function hasInfoNeededToCalculateTotal(Checkout $checkout) : bool

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件