PHP code example of ashadozzaman / coupon
1. Go to this page and download the library: Download ashadozzaman/coupon library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ashadozzaman / coupon example snippets bash
php artisan vendor:publish --provider="Ashadozzaman\Coupon\CouponServiceProvider"
bash
php artisan migrate
bash
Route::match(array('GET', 'POST'), 'checkout/{id?}', [HomeController::class,'checkout_course'])->name('checkout.course');
bash
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);
}
bash
//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" => "৳"
]
bash
//must be pass 2 perameter customer_id(login user id),coupon_code;
$response = $this->useCouponByUser($data['customer_id'],$request->coupon);
bash
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);
}
}
}