此包为各种课程或产品的优惠券/折扣生成...

v1.0.1 2022-09-23 12:32 UTC

This package is auto-updated.

Last update: 2024-09-26 08:43:20 UTC


README

此包为任何类型的课程或产品销售网站生成优惠券/折扣...🎟

Latest Version on Packagist Total Downloads
以下是如何创建优惠券的示例

安装

您可以通过composer安装此包

composer require ashadozzaman/coupon

现在您应该在config/app.php中注册CouponServiceProvider

'providers' => [
  Ashadozzaman\Coupon\CouponServiceProvider::class,
]

包将自动注册自己。

您可以使用以下命令发布迁移,它将生成config/coupon.php文件,并创建views/vendor/coupon文件夹以创建优惠券,如果您想的话,可以更改所有文件的设计。但不要破坏结构...

php artisan vendor:publish --provider="Ashadozzaman\Coupon\CouponServiceProvider"

以下显示config/coupon.php文件。在这里您设置必要的信息

<?php
return [
    //this table/model use for single coupon generate, like Product/products, Course/courses
    'coupon_for_single_table' => App\Models\Course::class,


    //this table/model use for specific category coupon generate, like Product/products, Course/courses
    'coupon_for_category_table' => App\Models\Category::class,


    //money symbol
    'money_symbol' => '',


    //is_admin middleware define here
    //'is_admin' => \App\Http\Middleware\IsAdmin::class, example default action this

    'IsAdmin' => 'is_admin',

];

迁移发布后,您可以通过运行迁移来创建优惠券表

php artisan migrate

用法

此包的基本概念是您可以通过管理员为特定课程/产品及其产品类别创建优惠券。要访问此功能,请在config/coupon.php文件中声明您的管理员身份验证中间件,如下所示:

return [
     --------
     --------
    'IsAdmin' => 'is_admin', //you should replace your middleware to is_admin thanks
]

模型声明

您必须声明与优惠券相关的项目模型,如products/coures和categories表模型,以下是一个示例:

<?php
return [
    //this table/model use for single coupon generate, like Product/products, Course/courses
    'coupon_for_single_table' => App\Models\Course::class,


    //this table/model use for specific category coupon generate, like Product/products, Course/courses
    'coupon_for_category_table' => App\Models\Category::class,
    
]

中间件注册完成后,现在您可以在管理员部分访问创建优惠券。现在您只需要调用两个路由来创建优惠券。以下显示两个路由:

Route name: route(coupon.index), Route Url: /coupon <br>
Route name: route(coupon_category.index), Route Url: /coupon_category
//use example
<a href="{{route('coupon.index')}}">Coupon</a><br>
<a href="{{route('coupon_category.index')}}">Coupon Category</a>

管理员优惠券创建流程在这里完成

在前端使用优惠券 🎟

优惠券通常用于结账页面。因此,您只需使用相同的路由在结账页面轻松使用优惠券。以下是一个示例代码:

route/web.php(仅示例,请按自己的方式编写代码)

Route::match(array('GET', 'POST'), 'checkout/{id?}', [HomeController::class,'checkout_course'])->name('checkout.course');

Http/Controllers/HomeController.php(仅示例,请按自己的方式编写代码)

在控制器中必须使用以下Trait:

use Ashadozzaman\Coupon\Http\Traits\CouponGenerate;
class HomeController extends Controller
{
    use CouponGenerate;
    --
    --
    --
 }

此函数在我的端上运行。这里仅作为示例。

public function checkout_course(Request $request,$id = null){
    $data['course'] = Course::findOrFail($id);
    $course = Course::findOrFail($id);
    if($request->coupon){
        $coupon = $request->coupon;
        $item = $id;
        $item_category = $course->category->id;
        $customer_id = auth()->user->id; //user, student, customer //login user

        //must be call with 4 perameter 1.coupon 2. coupon item id(course) 3.item category id 4.Customer id
        $response = $this->checkCoupunStatus($coupon,$item,$item_category,$customer_id);
        if($response['status'] == "error"){
            Session::flash('message',$response['message']);
            return redirect()->back();
        }else{
            $data['coupon'] = $response;
        }

    }
    return view('checkout',$data);

}

使用此trait函数检查优惠券状态,用于使用优惠券时间。必须传递所有必要参数。此函数返回数组类型的响应。在数组中传递两种类型的状态:1.成功 2.错误。

//must be call with 4 perameter 1.coupon 2. coupon item id(course) 3.item category id 4.Customer id //login user
$response = $this->checkCoupunStatus($coupon,$item,$item_category,$customer_id);

以下显示响应示例:

//error
array:2 [▼
  "status" => "error"
  "message" => "This coupon is not vaild for this product"
]
//success
array:6 [▼
  "status" => "success"
  "price" => 65.0
  "final_price" => 55.0
  "rate" => 10
  "coupon" => "O4BY-TSKO"
  "type" => ""
]

checkout.blade.php(仅示例,请按自己的方式编写代码)

<section>
  <h3><u> Course</u></h3>
  @if (Session::has('message'))
      <p class="alert alert-info">{{ Session::get('message') }}</p>
  @endif
  <div class="row">
      <div class="col-md-6">
          <form action="{{ route('submit.checkout') }}" method="post">
              @csrf
              <label for="">Name : {{ $course->name }}</label><br>
              <input type="hidden" name="course_id" value="{{$course->id}}">
              <label for="">Price : {{ $course->amount }}</label><br>
              <hr>
              @if (isset($coupon))
                  <label for="">Discount: - {{ $coupon['rate'] }} {{ $coupon['type'] }}</label><br>
                  <label for="">Final Price</label>
                  <input class="form-control" type="text" name="price"
                      value="{{ $coupon['final_price'] }}" readonly>
                  <label for="">Use Coupon</label>
                  <input class="form-control" type="text" name="coupon" value="{{ $coupon['coupon'] }}" readonly>
              @else
                  <label for="">Final Price</label><br>
                  <input class="form-control" type="text" name="price" value="{{ $course->amount }}"
                      readonly>
              @endif
              <button class="btn btn-success" type="submit">Checkout</button>
          </form>
      </div>
      <div class="col-md-6">
          <form action="{{ route('checkout.course', $course->id) }}" method="post">
              @csrf
              <label for="">Use Coupon</label>
              <input type="text" name="coupon" class="form-control" placeholder="Enter coupon">
              <button class="btn btn-primary btn-sm">Use Coupon</button>
          </form>
      </div>
  </div>
</section>

使用优惠券后,当您提交结账表单时,即保存结账详细信息时,您必须调用以下trait函数:

//must be pass 2 perameter customer_id(login user id),coupon_code;
$response = $this->useCouponByUser($data['customer_id'],$request->coupon);

使用此函数存储使用优惠券的用户和优惠券代码。因此必须在保存结账详细信息后调用此函数。以下显示示例细节:

 public function checkout_submit(Request $request){
      $data['customer_id'] = auth()->user->id;//login user id
      $data['course_id'] = $request->course_id; // course/product
      $data['price'] = $request->price;
      $booking = Booking::create($data);
      if(isset($booking)){
          if($request->coupon){
              //must be pass 2 perameter customer_id(login user id),coupon_code;
              $response = $this->useCouponByUser($data['customer_id'],$request->coupon);
          }
      }
  }

以下显示响应示例:

$data = [
    'status' => 'success',
    'message'  => 'Coupon use successfully',
];

贡献者

Ashadozzaman Shvou(shovoua@gmail.com)

支持

如有任何疑问,请发送电子邮件至 shovoua@gmail.com

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件