洛伦兹·伊普西兰蒂克·道尔·斯图尔特·阿梅特,康奈克图斯·阿迪宾迪克·埃利特,塞多·尤尔·伊休梅多·特莫里努斯·伊努迪恩·乌塔·埃图罗·伊图罗·马加卢阿。

v2.6.0 2024-07-18 14:45 UTC

README

alt text

电子商务购物车

Version Issues Licence

此软件包包含您在Laravel应用程序中实现购物车所需的所有逻辑。它还包含您管理商店所需的所有Nova资源。Marshmallow为许多客户使用此软件包,并在需要时添加新功能。如果您想使用此软件包,请这样做,并在遇到任何问题的情况下告知我们。

安装

Composer

您可以通过以下Composer命令安装此软件包。

composer require marshmallow/cart

迁移

您需要运行此软件包中的迁移以创建我们进行电子商务魔法所需的所有表。

php artisan migrate

中间件

请将以下中间件添加到您的Web组中,以确保购物车在每个路由上可用。如果您希望将此中间件包括在选定的路由集中,您也可以这样做。

protected $middlewareGroups = [
    'web' => [
        // ...
        \Marshmallow\Ecommerce\Cart\Http\Middleware\CartMiddleware::class,
    ],
];

Nova

运行以下命令以发布您需要管理所有电子商务内容所需的所有Nova资源。

php artisan marshmallow:resource Product Product
php artisan marshmallow:resource ProductCategory Product
php artisan marshmallow:resource Supplier Product
php artisan marshmallow:resource Price Priceable
php artisan marshmallow:resource VatRate Priceable
php artisan marshmallow:resource Currency Priceable
php artisan marshmallow:resource Prospect Ecommerce\\Cart
php artisan marshmallow:resource ShoppingCart Ecommerce\\Cart
php artisan marshmallow:resource Customer Ecommerce\\Cart
php artisan marshmallow:resource ShippingMethod Ecommerce\\Cart
php artisan marshmallow:resource ShippingMethodCondition Ecommerce\\Cart
php artisan marshmallow:resource Order Ecommerce\\Cart
php artisan marshmallow:resource OrderItem Ecommerce\\Cart
php artisan marshmallow:resource Discount Ecommerce\\Cart
php artisan marshmallow:resource Route Seoable

填充表

我们为荷兰的电子商务网站创建了种子器。如果您正在运营一家荷兰商店,可以运行这些种子器。如果不是,则不要运行这些种子器。只需通过您刚刚创建的Nova资源创建自己的种子器。

php artisan db:seed --class=Marshmallow\\Priceable\\Seeders\\CurrencySeeder
php artisan db:seed --class=Marshmallow\\Priceable\\Seeders\\VatRatesSeeder

环境文件

确保您在您的.env文件中将CURRENCY值设置为与您使用的货币相匹配。

CURRENCY=eur

事件

此软件包触发一系列事件,您可以在应用程序中监听这些事件(如果需要的话)。

折扣

设置

要使用折扣模块,您首先需要确保已运行所有migrations

创建Nova资源

您需要发布Nova资源,以便在Nova中创建新的折扣代码。运行以下命令。

php artisan marshmallow:resource Discount Ecommerce\\Cart

发布新配置

有一个新的配置文件处理折扣功能默认值。运行以下命令以发布新的配置文件。

php artisan vendor:publish --tag="ecommerce-discount-config"

用法

添加折扣

要将折扣添加到购物车,您需要创建自己的路由/端点来处理此操作。您可以使用以下示例代码来激活折扣。如果一切顺利,则$response将为空。如果出现错误,此方法将返回包含我们为什么无法将折扣添加到购物车的原因的错误消息。

use Marshmallow\Ecommerce\Cart\Facades\Cart;
use Marshmallow\Ecommerce\Cart\Models\Discount;

$discount = Discount::byCode(
    request()->discount
);

$response = Cart::get()->addDiscount($discount);

删除折扣

如果您允许客户从购物车中删除已激活的折扣,您将再次需要创建自己的路由/端点来处理此操作。然后,您可以使用以下示例代码从卡片中删除折扣。

use Marshmallow\Ecommerce\Cart\Facades\Cart;

Cart::get()->deleteDiscount();

购物车方法

随着折扣方法的引入,您可能需要更新用于显示购物车总金额的方法。请参阅以下新方法。

$cart->getTotalAmountWithoutShippingAndDiscount();
$cart->getTotalAmountWithoutShippingAndDiscountAndWithoutVat();
$cart->getDiscountAmount();
$cart->getDiscountAmountWithoutVat();

购物车方法

/**
 * These are helper functions to get cart totals.
 */
$cart->getTotalAmountWithoutShipping();
$cart->getTotalAmountWithoutShippingAndWithoutVat();
$cart->getShippingAmount();
$cart->getShippingAmountWithoutVat();
$cart->getTotalAmount();
$cart->getTotalAmountWithoutVat();
$cart->getTotalVatAmount();
$cart->getTotalAmountWithoutShippingAndDiscount();
$cart->getTotalAmountWithoutShippingAndDiscountAndWithoutVat();
$cart->getDiscountAmount();
$cart->getDiscountAmountWithoutVat();

/**
 * You can format all the methods above to get a string with currency.
 */
$cart->getFormatted('getTotalAmount');

/**
 * Extra helpers
 */
$cart->productCount();
$cart->getItemsWithoutShipping();
$cart->getItemsWithoutDiscount();
$cart->getDiscountItems();
$cart->getItemsWithoutDiscountAndShipping();
$cart->getOnlyProductItems();

项目方法

$item->setQuantity(4);
$item->increaseQuantity();
$item->decreaseQuantity();

// Amount helpers
$item->getUnitAmount();
$item->getUnitAmountWithVat();
$item->getUnitAmountWithoutVat();
$item->getUnitVatAmount();
$item->getTotalAmount();
$item->getTotalAmountWithVat();
$item->getTotalAmountWithoutVat();
$item->getTotalVatAmount();

// Formatted
$item->getFormatted('getTotalAmount');

变更日志

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

测试

composer test

安全

如果您发现任何与安全相关的问题,请通过电子邮件发送给 stef@marshmallow.dev,而不是使用问题跟踪器。

致谢

许可

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