承接 faysal0x1/laravelshoppingcart 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

faysal0x1/laravelshoppingcart

Composer 安装命令:

composer require faysal0x1/laravelshoppingcart

包简介

Laravel Shopping cart

README 文档

README

A powerful and flexible shopping cart implementation for Laravel 7, 8, 9, 10, and 11. This package provides a simple and elegant way to manage shopping carts in your Laravel applications, supporting multiple cart instances, tax calculations, and various integration methods.

Table of Contents

Installation

Install the package through Composer:

composer require faysal0x1/laravelshoppingcart

Laravel <= 7.0

For Laravel 7.0, add the service provider and alias in config/app.php:

'providers' => [
    // ...
    Gloudemans\Shoppingcart\ShoppingcartServiceProvider::class,
],

'aliases' => [
    // ...
    'Cart' => Gloudemans\Shoppingcart\Facades\Cart::class,
],

Configuration

Publish the configuration file:

php artisan vendor:publish --provider="Gloudemans\Shoppingcart\ShoppingcartServiceProvider"

This will create a config/cart.php file where you can customize:

  • Tax rate
  • Number format
  • Default cart instance
  • Database storage settings

Basic Usage

Adding Items

// Basic usage
Cart::add('293ad', 'Product 1', 1, 9.99);

// With options
Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']);

// Using array
Cart::add([
    'id' => '293ad',
    'name' => 'Product 1',
    'qty' => 1,
    'price' => 9.99,
    'options' => ['size' => 'large']
]);

// Using Buyable interface
Cart::add($product, 1, ['size' => 'large']);

Updating Items

$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

// Update quantity
Cart::update($rowId, 2);

// Update item details
Cart::update($rowId, ['name' => 'New Product Name']);

// Update with Buyable
Cart::update($rowId, $product);

Removing Items

Cart::remove($rowId);

Getting Cart Content

// Get all items
$items = Cart::content();

// Get specific item
$item = Cart::get($rowId);

// Get cart total
$total = Cart::total();

// Get tax amount
$tax = Cart::tax();

// Get subtotal
$subtotal = Cart::subtotal();

// Get item count
$count = Cart::count();

Integration Methods

Blade Templates

// In your controller
public function index()
{
    $cart = Cart::content();
    return view('cart.index', compact('cart'));
}
{{-- In your blade view --}}
@foreach(Cart::content() as $item)
    <div class="cart-item">
        <h3>{{ $item->name }}</h3>
        <p>Quantity: {{ $item->qty }}</p>
        <p>Price: {{ $item->price }}</p>
        @if($item->options->has('size'))
            <p>Size: {{ $item->options->size }}</p>
        @endif
    </div>
@endforeach

<div class="cart-total">
    <p>Subtotal: {{ Cart::subtotal() }}</p>
    <p>Tax: {{ Cart::tax() }}</p>
    <p>Total: {{ Cart::total() }}</p>
</div>

Inertia.js

// In your controller
public function index()
{
    return Inertia::render('Cart/Index', [
        'cart' => [
            'items' => Cart::content(),
            'subtotal' => Cart::subtotal(),
            'tax' => Cart::tax(),
            'total' => Cart::total(),
        ]
    ]);
}
<!-- In your Vue component -->
<template>
  <div>
    <div v-for="item in cart.items" :key="item.rowId" class="cart-item">
      <h3>{{ item.name }}</h3>
      <p>Quantity: {{ item.qty }}</p>
      <p>Price: {{ item.price }}</p>
      <p v-if="item.options.size">Size: {{ item.options.size }}</p>
    </div>
    
    <div class="cart-total">
      <p>Subtotal: {{ cart.subtotal }}</p>
      <p>Tax: {{ cart.tax }}</p>
      <p>Total: {{ cart.total }}</p>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    cart: Object
  }
}
</script>

API Integration

// In your API controller
public function addToCart(Request $request)
{
    $validated = $request->validate([
        'id' => 'required',
        'name' => 'required',
        'qty' => 'required|numeric|min:1',
        'price' => 'required|numeric|min:0',
        'options' => 'array'
    ]);

    Cart::add($validated);

    return response()->json([
        'message' => 'Item added to cart',
        'cart' => [
            'items' => Cart::content(),
            'total' => Cart::total()
        ]
    ]);
}

public function getCart()
{
    return response()->json([
        'items' => Cart::content(),
        'subtotal' => Cart::subtotal(),
        'tax' => Cart::tax(),
        'total' => Cart::total()
    ]);
}

Advanced Features

Multiple Cart Instances

// Create a wishlist instance
Cart::instance('wishlist')->add('293ad', 'Product 1', 1, 9.99);

// Get wishlist content
$wishlist = Cart::instance('wishlist')->content();

Database Storage

Enable database storage in config/cart.php:

'storage' => 'database',

Run migrations:

php artisan migrate

Events

The package fires several events you can listen to:

// In your EventServiceProvider
protected $listen = [
    'cart.added' => [
        'App\Listeners\CartItemAdded',
    ],
    'cart.updated' => [
        'App\Listeners\CartItemUpdated',
    ],
    'cart.removed' => [
        'App\Listeners\CartItemRemoved',
    ],
];

Testing

Run the test suite:

composer test

Contributing

Please see CONTRIBUTING.md for details.

License

The MIT License (MIT). Please see License File for more information.

faysal0x1/laravelshoppingcart 适用场景与选型建议

faysal0x1/laravelshoppingcart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 04 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「shoppingcart」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 faysal0x1/laravelshoppingcart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 faysal0x1/laravelshoppingcart 我们能提供哪些服务?
定制开发 / 二次开发

基于 faysal0x1/laravelshoppingcart 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 31
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-20