yabhq/laravel-cart 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yabhq/laravel-cart

最新稳定版本:v0.20.1

Composer 安装命令:

composer require yabhq/laravel-cart

包简介

Simple yet customizable Laravel shopping cart

README 文档

README

Latest Version on Packagist CircleCI

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.

统计信息

  • 总下载量: 3.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 20
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 20
  • Watchers: 2
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固