treestoneit/shopping-cart

Laravel 6/7/8 的简单易用购物车

v1.5.0 2022-09-13 20:54 UTC

This package is auto-updated.

Last update: 2024-09-14 01:29:46 UTC


README

Software License Latest Version on Packagist Total Downloads Build Status StyleCI Scrutinizer Code Quality

这是一个为 Laravel 6/7/8 简单实现的购物车。它会自动将购物车序列化到数据库中,并加载相关的产品模型。

用法

要开始,请将 Buyable 接口添加到您的模型中。

use Illuminate\Database\Eloquent\Model;
use Treestoneit\ShoppingCart\Buyable;
use Treestoneit\ShoppingCart\BuyableTrait;

class Product extends Model implements Buyable
{
    use BuyableTrait;
}

确保您实现了 getBuyableDescriptiongetBuyablePrice 方法,分别用于产品描述和产品价格。

现在您可以向购物车添加产品了。

use Treestoneit\ShoppingCart\Facades\Cart;

$product = Product::create(['name' => 'Pizza Slice', 'price' => 1.99]);
$quantity = 2;

Cart::add($product, $quantity);

要检索购物车内容

Cart::content();
// or
Cart::items();

要检索总额

Cart::subtotal();

您可以更新购物车中商品的数量。第一个参数是相关 CartItem 的主键。

$item = Cart:content()->first();

Cart::update($item->id, $item->quantity + 5);

或者完全删除该商品。

Cart::remove($item->id);

选项

要将特定于商品的选项(如尺寸或颜色)添加到购物车中的商品,首先在您的 Buyable 实例中注册可用选项。

class Product extends Model implements Buyable
{
    // ...
    
    public function getOptions(): array
    {
        return [
            'size' => ['18 inch', '36 inch'],
            'color' => ['white', 'blue', 'black'],
        ];
    }
}

然后,只需将关联数组作为 Cart::add 的第三个参数传递。

Cart::add($product, 3, ['color' => 'white']);

任何无效的选项都将从数组中静默删除。

您还可以通过调用 Cart::updateOption 来添加或更改购物车中现有商品的选项。

$item = Cart:content()->first();

// Update a single option
Cart::updateOption($item->id, 'color', 'black');

// Update multiple options at once
Cart::updateOptions($item->id, [
    'color' => 'black',
    'size' => '36 inch',
]);

选项数组将在 CartItem 实例上作为 $item->options 可用。

关联到用户

您可以将购物车实例关联到一个用户,以便检索他们之前会话中的购物车。通过调用 attachTo 方法并将 Illuminate\Contracts\Auth\Authenticatable 实例传递进来,来实现将购物车关联到用户。

class RegisterController
{
    /**
     * The user has been registered.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function registered(Request $request, $user)
    {
        Cart::attachTo($user);
    }
}

然后当用户登录时,您可以通过再次传递用户实例来调用 loadUserCart 方法。

class LoginController
{
    /**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function authenticated(Request $request, $user)
    {
        Cart::loadUserCart($user);
    }
}

依赖注入

如果您不是门面派,可以使用容器通过类型提示 Treestoneit\ShoppingCart\CartManager 类或 Treestoneit\ShoppingCart\CartContract 接口来注入购物车实例。

购物车可以计算购物车中商品的总税额。只需调用

$rate = 13; // The tax rate as a percentage

Cart::tax($rate);

您还可以在包含的配置文件中设置默认税率。

// config/shopping-cart.php

    'tax' => [
        'rate' => 6,
    ],

然后只需调用 Cart::tax 而不带参数。

Cart::tax();

如果您的某些商品有不同的适用税率或免税,没问题。首先修改配置文件

// config/shopping-cart.php

    'tax' => [
        'mode' => 'per-item',
    ],

然后,通过实现 Taxable 接口并定义 getTaxRate 方法来设置每个商品的税率。

use Treestoneit\ShoppingCart\Taxable;

class Product extends Model implements Buyable, Taxable
{
    /**
     * Calculate the tax here based on a database column, or whatever you will.
     *
     * @return int|float
     */
    public function getTaxRate()
    {
        if ($this->tax_rate) {
            return $this->tax_rate;
        }

        if (! $this->taxable) {
            return 0;
        }

        return 8;
    }

现在在调用 Cart::tax 时,您的商品将应用自定义税率。

安装

您可以通过 composer 安装此包

composer require treestoneit/shopping-cart

要发布配置文件和迁移,运行

php artisan vendor:publish --provider="Treestoneit\ShoppingCart\CartServiceProvider"

并运行包含的数据库迁移。

php artisan migrate

测试

composer test

入门(演示)仓库

如果您想查看使用此购物车的入门/演示实现,请查看我们的 laravel-commerce 仓库

路线图

我还没有完成的几件事

  • 在会话销毁时清除未关联到用户的购物车实例。
  • 添加一个 Artisan 命令,用于清除任何未关联的购物车(这两个可能互斥)
  • 当调用 loadUserCart 时,增加配置购物车合并策略的功能

致谢

许可协议

MIT 许可协议(MIT)。请参阅许可文件获取更多信息。

Laravel 包模板

本包是用 Laravel 包模板 生成的。