php - Applying discount code only once for each user
Get the solution ↓↓↓I have a form of e-commerce like this
All you have to do is create a discount code system.
We have three user tables of discount codes and orders ... Implement in such a way that each user can use the relevant discount code only once.
Suppose an order is registered with a certain amount that after hitting that discount code .. That discount amount is applied as a percentage of the method and as mentioned, each user can use that discount code once.
There is no need for Front at all ... A very simple page view so that your work can be seen,
This is my main problem, How to apply the discount code only once for each user?
products
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('price');
$table->boolean('stock')->default(0);
$table->string('image');
$table->timestamps();
});
}
coupons
public function up()
{
Schema::create('coupons', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('code');
$table->timestamps();
});
Schema::create('coupon_user', function (Blueprint $table) {
$table->foreignId('coupon_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
});
}
orders
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->boolean('status')->default(0);
$table->string('price');
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->timestamps();
});
Schema::create('order_product', function (Blueprint $table) {
$table->foreignId('order_id')->constrained()->cascadeOnDelete();
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
});
}
blade
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Dashboard') }}</div>
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
<div class="row">
@foreach($products as $product)
<div class="col-6">
<div class="card">
<img src="{{ asset('image/'.$product->image) }}" class="card-img-top" alt="{{ $product->title }}">
<div class="d-flex justify-content-between">
<div class="card-body col-6">
<h5 class="card-title">{{ $product->title }}</h5>
<p class="card-text">{{ $product->price }}</p>
<p class="card-text">{{ $product->stock == 1 ? 'Stock' : 'Un Stock' }}</p>
@if($product->stock == 1)
<form action="{{ route('order') }}" method="post" class="mb-3">
<input type="hidden" name="product_id" value="{{ $product->id }}">
<input type="hidden" name="price" value="{{ $product->price }}">
@csrf
<button type="submit" class="btn btn-success">Add to cart</button>
</form>
@endif
</div>
<form action="{{ route('coupon') }}" method="post">
@csrf
<label for="code">Coupon Code</label>
<div class="form-group">
<input type="text" name="code" id="code" class="form-control">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Apply</button>
</div>
</form>
</div>
</div>
</div>
@endforeach
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
web.php
Route::get('/home', 'HomeController@index')->name('home');
Route::post('/order', 'HomeController@order')->name('order');
Route::post('/coupon', 'HomeController@coupon')->name('coupon');
HomeController
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$products = Product::all();
return view('home', compact('products'));
}
public function order(Request $request)
{
$order = Order::create([
'user_id' => auth()->id(),
'price' => $request->price,
]);
$order->products()->attach($request->product_id);
return redirect()->back();
}
public function coupon(Request $request)
{
$validated = $request->validate([
'code' => 'required'
]);
$count = Order::where('user_id', auth()->id())->where('status', 1)->count();
if (exists($validated)) {
}
if ($count > 1) {
}
}
Answer
Solution:
Just Check the coupon_user table when a user applies for a coupon. Make a model for Coupon User. Use the model in the Home controller. Then change the code in the coupon function.
public function coupon(Request $request)
{
$validated = $request->validate([
'code' => 'required'
]);
$user_id = auth()->id();
$coupon_user = CouponUser::where('user_id', $user_id)->first();
if($coupon_user)
{
return redirect()->back()->with("error", "You have already applied this coupon");
}
// else do just whatever you need.
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: composer detected issues in your platform: your composer dependencies require a php version ">= 7.3.0".
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.