added card toggle

added card storage to customer
master
A1Gard 2 months ago
parent d7292f7457
commit 15ca8ce927

@ -30,7 +30,7 @@ class ClientController extends Controller
public function post(Post $post)
{
if ($post->status = 0 && !auth()->check()){
if ($post->status = 0 && !auth()->check()) {
return abort(403);
}
$area = 'post';
@ -39,14 +39,15 @@ class ClientController extends Controller
$post->increment('view');
return view('client.post', compact('area', 'post', 'title', 'subtitle'));
}
public function gallery(Gallery $gallery)
{
if ($gallery->status = 0 && !auth()->check()){
if ($gallery->status = 0 && !auth()->check()) {
return abort(403);
}
$area = 'gallery';
$title = $gallery->title;
$subtitle = \Str::limit(strip_tags($gallery->description),15);
$subtitle = \Str::limit(strip_tags($gallery->description), 15);
$gallery->increment('view');
return view('client.gallery', compact('area', 'gallery', 'title', 'subtitle'));
}
@ -60,6 +61,7 @@ class ClientController extends Controller
->orderByDesc('id')->paginate($this->paginate);
return view('client.default-list', compact('area', 'posts', 'title', 'subtitle'));
}
public function products()
{
$area = 'products-list';
@ -133,23 +135,91 @@ class ClientController extends Controller
}
public function group(Group $group){
public function group(Group $group)
{
$area = 'group';
$title = $group->name;
$subtitle = $group->subtitle;
$posts = $group->posts()->orderByDesc('id')->paginate($this->paginate);
return view('client.group', compact('area', 'posts', 'title', 'subtitle','group'));
return view('client.group', compact('area', 'posts', 'title', 'subtitle', 'group'));
}
public function attachDl(Attachment $attachment){
public function attachDl(Attachment $attachment)
{
$attachment->increment('downloads');
$file = (storage_path().'/app/public/attachments/'. $attachment->file);
$file = (storage_path() . '/app/public/attachments/' . $attachment->file);
if (file_exists($file)) {
return response()->download($file);
}
}
public function productCardToggle(Product $product)
{
$quantity = \request()->input('quantity', null);
if (\Cookie::has('card')) {
$cards = json_decode(\Cookie::get('card'),true);
$qs = json_decode(\Cookie::get('q'),true);
if (in_array($product->id, $cards)) {
$msg = "Product removed from card";
$i = array_search($product->id, $cards);
unset($cards[$i]);
unset($qs[$i]);
} else {
$cards[] = $product->id;
$qs[] = $quantity;
$msg = "Product added to card";
}
$count = count($cards);
\Cookie::queue('card', json_encode($cards), 2000);
\Cookie::queue('q', json_encode($qs), 2000);
} else {
$count = 1;
$msg = "Product added to card";
\Cookie::queue('card', "[$product->id]", 2000);
\Cookie::queue('q', "[$quantity]", 2000);
$qs = [$quantity];
$cards = [$product->id];
}
if ($count > 0 && auth('customer')->check()) {
$customer = auth('customer')->user();
$customer->card = json_encode(['cards' => $cards, 'quantities' => $qs]);
$customer->save();
}
if (\request()->ajax()) {
return success(['count' => $count], $msg);
} else {
return redirect()->back()->with(['message' => $msg]);
}
}
public function productCompareToggle(Product $product)
{
if (\Cookie::has('compares')) {
$compares = json_decode(\Cookie::get('compares'),true);
if (in_array($product->id, $compares)) {
$msg = "Product removed from compare";
unset($compares[array_search($product->id, $compares)]);
} else {
$compares[] = $product->id;
$msg = "Product added to compare";
}
\Cookie::queue('compares', json_encode($compares), 2000);
} else {
$msg = "Product added to compare";
\Cookie::queue('compares', "[$product->id]", 2000);
}
if (\request()->ajax()) {
return success(null, $msg);
} else {
return redirect()->back()->with(['message' => $msg]);
}
}
public function ProductFavToggle(Product $product)
{
if (!auth('customer')->check()) {

@ -25,6 +25,7 @@ return new class extends Migration
$table->boolean('colleague')->default(false);
$table->text('description')->default(null)->nullable();
$table->bigInteger('credit')->default(0);
$table->json('card')->default(null)->nullable();
$table->rememberToken();
$table->timestamps();
$table->softDeletes();

@ -1,11 +1,39 @@
window.addEventListener('load', function () {
const favUrl = document.querySelector('#api-fav-toggle').value;
const compUrl = document.querySelector('#api-compare-toggle').value;
document.querySelectorAll('.fav-btn')?.forEach(function (el) {
el.addEventListener('click', async function () {
let resp = await axios.get(favUrl+this.getAttribute('data-slug'));
if (resp.data.success){
this.setAttribute('data-is-fav',resp.data.data);
window.$toast.success(resp.data.message);
}else {
window.$toast.error("Error!");
}
});
});
document.querySelectorAll('.compare-btn')?.forEach(function (el) {
el.addEventListener('click', async function () {
let resp = await axios.get(compUrl+this.getAttribute('data-slug'));
if (resp.data.success){
window.$toast.success(resp.data.message);
}else {
window.$toast.error("Error!");
}
});
});
document.querySelectorAll('.add-to-card')?.forEach(function (el) {
el.addEventListener('click', async function (e) {
e.preventDefault();
let resp = await axios.get(this.getAttribute('href'));
if (resp.data.success){
window.$toast.success(resp.data.message);
document.querySelectorAll('.card-count')?.forEach(function (el2) {
el2.innerText = resp.data.data.count;
});
}else {
window.$toast.error("Error!");
}
});
});

@ -33,7 +33,7 @@
{{$product->getPrice()}}
</div>
<a href="" class="btn btn-primary btn-sm w-100">
<a href="{{ route('client.product-card-toggle',$product->slug) }}" class="btn btn-primary btn-sm w-100 add-to-card">
<i class="ri-shopping-cart-2-line"></i>
<span>
Add to card

@ -1,7 +1,8 @@
</div>
@yield('custom-foot')
<input type="hidden" id="api-display-url" value="{{route('v1.visitor.display')}}">
<input type="hidden" id="api-fav-toggle" value="{{route('client.client.product-fav-toggle','')}}/">
<input type="hidden" id="api-fav-toggle" value="{{route('client.product-fav-toggle','')}}/">
<input type="hidden" id="api-compare-toggle" value="{{route('client.product-compare-toggle','')}}/">
{{--@if(auth()->check() && auth()->user()->hasRole('developer') && !request()->has('ediable'))--}}
{{--<a id="do-edit" href="?ediable">--}}

@ -368,7 +368,9 @@ Route::name('client.')->group(function (){
Route::get('attach/download/{attachment}', [\App\Http\Controllers\ClientController::class,'attachDl'])->name('attach-dl');
Route::get('/post/{post}', [\App\Http\Controllers\ClientController::class,'post'])->name('post');
Route::get('product/fav/toggle/{product}', [\App\Http\Controllers\ClientController::class, 'ProductFavToggle'])->name('client.product-fav-toggle');
Route::get('product/fav/toggle/{product}', [\App\Http\Controllers\ClientController::class, 'ProductFavToggle'])->name('product-fav-toggle');
Route::get('product/compare/toggle/{product}', [\App\Http\Controllers\ClientController::class, 'productCompareToggle'])->name('product-compare-toggle');
Route::get('card/toggle/{product}', [\App\Http\Controllers\ClientController::class, 'productCardToggle'])->name('product-card-toggle');
Route::post('/comment/submit', [\App\Http\Controllers\ClientController::class,'submitComment'])->name('comment.submit');
})->middleware([\App\Http\Middleware\VisitorCounter::class]);

Loading…
Cancel
Save