pixellair/laravel-discount-system 问题修复 & 功能扩展

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

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

pixellair/laravel-discount-system

最新稳定版本:v1.0.0

Composer 安装命令:

composer require pixellair/laravel-discount-system

包简介

Laravel discount system

README 文档

README

A reusable, Composer-installable coupon-based discount system for Laravel projects.

Easily integrates into any Laravel app to provide:

  • 🎯 Percentage or fixed amount discounts
  • ⏳ Time-limited discounts
  • 🔂 One-time use or limited usage
  • 👤 Per-user restrictions (only once per user)
  • 👥 Coupons for specific users only
  • 💵 Minimum order amount validation
  • 🧠 Smart code generation with type prefixes

🚀 Installation

1. Require via Composer

composer require pixellair/laravel-discount-system

2. Publish and Run Migrations

To create the necessary tables for storing discounts and their usage:

php artisan vendor:publish --tag=laravel-discount-system-migrations
php artisan migrate

🔧 Setup (If Needed)

If you're using Laravel 8+ with package auto-discovery, no setup is needed.

If not, register the service provider manually in config/app.php:

'providers' => [
    DiscountSystem\DiscountSystemServiceProvider::class,
],

###⚙️ Usage Apply a Coupon to an Order

use DiscountSystem\Services\DiscountService;

$service = new DiscountService();

$result = $service->apply($couponCode, $userId, $orderAmount);

$discountAmount = $result['amount'];
$discount = $result['discount'];

Record a Usage After Applying

$service->recordUsage($discount, $userId);

🧪 Validations Performed in apply()

✅ Coupon exists

⏳ Coupon is active (start & end time)

🔂 Usage limit not exceeded

👤 Coupon not already used by this user (if once_per_user)

👥 User is in allowed list (if specific_user_ids set)

💵 Order meets min_order_amount requirement

If any condition fails, a ValidationException is thrown with a meaningful message.

🧾 Code Generation Generate a prefixed coupon code for any type:

$code = $discountService->generateCode('T'); // e.g., T7K92LA

The prefix lets you categorize discounts (e.g., T for time-limited, A for amount-based).

💡 Example Controller Usage

use DiscountSystem\Services\DiscountService;

public function applyCoupon(Request $request)
{
    $service = new DiscountService();

    try {
        $result = $service->apply($request->code, auth()->id(), $order->total);

        $order->discount_amount = $result['amount'];
        $order->save();

        $service->recordUsage($result['discount'], auth()->id());

        return response()->json(['success' => true, 'discount' => $result['amount']]);
    } catch (\Illuminate\Validation\ValidationException $e) {
        return response()->json(['error' => $e->getMessage()], 422);
    }
}

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-16