ozdemir/aurora

Laravel的购物车

资助包维护!
n1crack

2.0.1 2023-12-07 16:09 UTC

This package is auto-updated.

Last update: 2024-09-08 20:43:37 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub

Aurora Cart是一个灵活且功能丰富的Laravel购物车库。

特性

  • 购物车管理:轻松管理游客和认证用户的购物车。
  • 添加项目:支持数量和选项,将项目添加到购物车中。
  • 修改项目:调整项目数量、删除项目、更新选项。
  • 计算器:实现用于运输、税费或任何其他额外费用的自定义计算器。
  • 元数据:将元信息附加到整个购物车或单个项目。
  • 快照和回滚:在创建订单等场景中保存和恢复购物车状态。
  • 验证:使用校验和验证购物车完整性。

安装

使用Composer安装此包

composer require ozdemir/aurora

配置

为了配置Aurora Cart,发布配置文件

php artisan vendor:publish --tag="aurora-config"

这将在您的配置目录中创建一个cart.php文件。以下是一个带说明的示例配置文件

// config/cart.php

use Ozdemir\Aurora\Cart;
use Ozdemir\Aurora\Generators\GenerateChecksum;
use Ozdemir\Aurora\Generators\GenerateSessionKey;
use Ozdemir\Aurora\Storages\SessionStorage;

return [
    'instance' => 'cart',

    'cart_class' => Cart::class,

    'storage' => SessionStorage::class,

    'cache_store' => env('CART_STORE', config('cache.default')),

    'monetary' => [
        'precision' => env('CART_CURRENCY_PRECISION', 2),
    ],

    'session_key_generator' => GenerateSessionKey::class,

    'checksum_generator' => GenerateChecksum::class,

    'calculate_using' => [
        // Custom calculators go here
    ],
];

基本用法

// Create a product class implementing the Sellable interface
class SellableProduct implements Sellable
{
    use SellableTrait;
}
// Adding an item to the cart
$product = new SellableProduct(); // Replace with your actual product model
$cartItem = new CartItem($product, quantity: 2);
Cart::add($cartItem);

// Retrieving cart information
$total = Cart::total();
$itemCount = Cart::count();
$items = Cart::items();
$itemQuantity = Cart::quantity(); // total quantity

// Adding an item with options to the cart
Cart::add(
       (new CartItem($product, quantity: 1))->withOption('color', 'blue')
                 ->withOption('material', 'metal', price: 5)
                 ->withOption('size', 'large', weight: 4)
         );

// Updating item quantity
Cart::update($cartItem->hash(), quantity: 3);

// Removing an item from the cart
Cart::remove($cartItem->hash());

Aurora Cart支持用于计算总金额的自定义计算器。您可以将自定义计算器添加到config/cart.php文件中的calculate_using键下。

示例

return [
    // ...
    'calculate_using' => [
        // discounts etc..
        ShippingCalculator::class
        TaxCalculator::class
    ],
];
class ShippingCalculator
{
    public function handle($payload, Closure $next)
    {
        [$price, $breakdowns] = $payload;
        
        $shippingPrice = Shipping::first()->amount;

        $price = $price + $shippingPrice;

        $breakdowns[] = [
            'type' => 'shipping',
            'amount' => $shippingPrice,
            // any additional values..
        ];

        return $next([$price, $breakdowns]);
    }
}
class TaxCalculator
{
    public function handle($payload, Closure $next)
    {
        [$price, $breakdowns] = $payload;
        
        $taxPrice = Tax::first()->amount;

        $price = $price + $taxPrice;

        $breakdowns[] = [
            'type' => 'tax',
            'amount' => $taxPrice,
            // any additional values..
        ];

        return $next([$price, $breakdowns]);
    }
}

现在,您的购物车将使用这些自定义计算器来计算总金额,包括运输和税费。根据您的具体业务需求调整计算器的逻辑。

分解

您可以使用Cart::breakdowns()方法检索购物车的分解情况。分解提供了详细的摘要,说明了总金额是如何计算的,包括来自运输、税费以及您添加的任何自定义计算器的贡献。

示例

$breakdowns = Cart::breakdowns();

// Output the breakdowns
print_r($breakdowns);

breakdowns()方法返回一个数组,其中每个元素代表一个分解项目。每个分解项目通常包括标签和值,提供了如何不同因素贡献于总体总金额的透明度。

以下是一个假设的示例输出

Array (
    [0] => Array (
        [label] => Shipping
        [value] => 10
    )
    [1] => Array (
        [label] => Tax
        [value] => 15
    )
    // Additional breakdown items based on your custom calculators
    // ...
)

根据您的具体用例调整输出格式和内容。

变更日志

有关最近更改的更多信息,请参阅变更日志

致谢

许可

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