yabhq/laravel-cart
Composer 安装命令:
composer require yabhq/laravel-cart
包简介
Simple yet customizable Laravel shopping cart
README 文档
README
Laravel Shopping Cart
A simple yet customizable Laravel shopping cart implementation.
Provides RESTful API endpoints out of the box to help with front-end / SPA integrations.
Table of Contents
Requirements
Installation
Usage
The Checkout Class
Customization
License
Requirements
- PHP 8+
- Laravel 8.x
Installation
composer require yabhq/laravel-cart
The package publishes some migrations, routes (for optional use) and classes for further customizing your store logistics.
php artisan vendor:publish --provider="Yab\ShoppingCart\ShoppingCartServiceProvider"
Full list of published files:
- 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
Usage
First, simply implement the Purchaseable interface on your product (or other purchaseable) model.
app/Models/Product.php
use Yab\ShoppingCart\Traits\Purchaseable; use Yab\ShoppingCart\Contracts\Purchaseable as PurchaseableInterface; class Product extends Model implements PurchaseableInterface { use Purchaseable; }
Next we should implement the Purchaser interface on the model representing the end customer.
app/Models/Customer.php
use Yab\ShoppingCart\Traits\Purchaser; use Yab\ShoppingCart\Contracts\Purchaser as PurchaserInterface; class Customer extends Model implements PurchaserInterface { use Purchaser; }
If you would like to use the built-in cart API endpoints, you can simply include the published checkout.php in your existing routes file.
routes/api.php (optional)
Route::group(['middleware' => ['example']], function () { require base_path('routes/checkout.php'); });
The Checkout Class
The package comes with a Checkout class which allows you to interact with the shopping cart.
use Yab\ShoppingCart\Checkout;
Creating or retrieving a checkout instance:
$checkout = Checkout::create(); // or $checkout = Checkout::findById('uuid-123');
Getting the ID of an existing checkout:
$checkout->id();
Adding a custom field for a checkout:
$checkout->setCustomField('some key', 'some value');
Deleting a checkout:
$checkout->destroy();
Interacting with the underlying cart model and query builder:
// Yab\ShoppingCart\Models\Cart $checkout->getCart(); // Illuminate\Database\Eloquent\Builder $checkout->getCartBuilder();
Adding, updating or removing cart items:
// 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);
Optionally set a purchaser entity (class must implement Purchaser interface):
$checkout->setPurchaser($customer);
Getting the shipping, subtotal, taxes and total:
$checkout->getShipping(); // 5.00 $checkout->getSubtotal(); // 110.00 $checkout->getDiscount(); // 10.00 $checkout->getTaxes(); // 13.00 $checkout->getTotal(); // 113.00
Customization
Not every e-commerce store is the same. This package provides several "logistics" classes which allow you to hook into the core package logic and perform some common customizations. For example, you may specify how the tax, shipping and discount amounts are determined:
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
License
The MIT License (MIT). Please see License File for more information.
yabhq/laravel-cart 适用场景与选型建议
yabhq/laravel-cart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.04k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2020 年 12 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cart」 「laravel」 「shopping」 「shopping cart」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yabhq/laravel-cart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yabhq/laravel-cart 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yabhq/laravel-cart 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel 11 and above Shopping cart
Admin Hub for GetCandy. A modern headless e-commerce solution for Laravel PHP framework.
universal shopping basket
Google Shopping Feed API (with ns problem fixed)
A PHP (and Laravel) package to interface with the Snipcart api.
Yii2 extension that adds shopping cart functions
统计信息
- 总下载量: 3.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-15