added card toggle

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

@ -39,6 +39,7 @@ class ClientController extends Controller
$post->increment('view'); $post->increment('view');
return view('client.post', compact('area', 'post', 'title', 'subtitle')); return view('client.post', compact('area', 'post', 'title', 'subtitle'));
} }
public function gallery(Gallery $gallery) public function gallery(Gallery $gallery)
{ {
if ($gallery->status = 0 && !auth()->check()) { if ($gallery->status = 0 && !auth()->check()) {
@ -60,6 +61,7 @@ class ClientController extends Controller
->orderByDesc('id')->paginate($this->paginate); ->orderByDesc('id')->paginate($this->paginate);
return view('client.default-list', compact('area', 'posts', 'title', 'subtitle')); return view('client.default-list', compact('area', 'posts', 'title', 'subtitle'));
} }
public function products() public function products()
{ {
$area = 'products-list'; $area = 'products-list';
@ -133,7 +135,8 @@ class ClientController extends Controller
} }
public function group(Group $group){ public function group(Group $group)
{
$area = 'group'; $area = 'group';
$title = $group->name; $title = $group->name;
$subtitle = $group->subtitle; $subtitle = $group->subtitle;
@ -141,7 +144,8 @@ class ClientController extends Controller
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'); $attachment->increment('downloads');
$file = (storage_path() . '/app/public/attachments/' . $attachment->file); $file = (storage_path() . '/app/public/attachments/' . $attachment->file);
if (file_exists($file)) { if (file_exists($file)) {
@ -150,6 +154,72 @@ class ClientController extends Controller
} }
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) public function ProductFavToggle(Product $product)
{ {
if (!auth('customer')->check()) { if (!auth('customer')->check()) {

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

@ -1,11 +1,39 @@
window.addEventListener('load', function () { window.addEventListener('load', function () {
const favUrl = document.querySelector('#api-fav-toggle').value; const favUrl = document.querySelector('#api-fav-toggle').value;
const compUrl = document.querySelector('#api-compare-toggle').value;
document.querySelectorAll('.fav-btn')?.forEach(function (el) { document.querySelectorAll('.fav-btn')?.forEach(function (el) {
el.addEventListener('click', async function () { el.addEventListener('click', async function () {
let resp = await axios.get(favUrl+this.getAttribute('data-slug')); let resp = await axios.get(favUrl+this.getAttribute('data-slug'));
if (resp.data.success){ if (resp.data.success){
this.setAttribute('data-is-fav',resp.data.data); this.setAttribute('data-is-fav',resp.data.data);
window.$toast.success(resp.data.message); 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()}} {{$product->getPrice()}}
</div> </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> <i class="ri-shopping-cart-2-line"></i>
<span> <span>
Add to card Add to card

@ -1,7 +1,8 @@
</div> </div>
@yield('custom-foot') @yield('custom-foot')
<input type="hidden" id="api-display-url" value="{{route('v1.visitor.display')}}"> <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'))--}} {{--@if(auth()->check() && auth()->user()->hasRole('developer') && !request()->has('ediable'))--}}
{{--<a id="do-edit" href="?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('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('/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'); Route::post('/comment/submit', [\App\Http\Controllers\ClientController::class,'submitComment'])->name('comment.submit');
})->middleware([\App\Http\Middleware\VisitorCounter::class]); })->middleware([\App\Http\Middleware\VisitorCounter::class]);

Loading…
Cancel
Save