定制 faysal0x1/laravelshoppingcart 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

faysal0x1/laravelshoppingcart

最新稳定版本:v2.3.7

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.

统计信息

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

GitHub 信息

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

其他信息

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