File size: 4,891 Bytes
52d9fb0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | <?php
namespace App\Http\Controllers;
use App\Constants\Status;
use App\Models\Cart;
use App\Models\Product;
use App\Models\ProductCollection;
use Illuminate\Http\Request;
class CartController extends Controller
{
public function index()
{
$pageTitle = 'Cart';
$sessionId = request()->session()->getId();
$cartItems = [];
if (auth() && auth()->check()) {
$cartItems = Cart::where('user_id', auth()->id())->with('product')->get();
} else {
$cartItems = Cart::where('session_id', $sessionId)->with('product')->get();
}
return view('Template::cart', compact('pageTitle', 'cartItems'));
}
public function store(Request $request)
{
$request->validate([
'license' => 'required',
'product_id' => 'required|exists:products,id',
]);
if (!in_array($request->license, [1, 2])) {
return response()->json(['status' => 'error', 'message' => 'Invalid license']);
}
$cartItems = $request->session()->get('cart', []);
$product = Product::approved()->find($request->product_id);
$newCartItem = $this->getDataForCart($product, null, null, $request);
if (!$newCartItem) {
$response = ['status' => 'error', 'message' => 'Item already added to cart'];
return $request->ajax() ? response()->json($response) : back()->withNotify([$response]);
}
$cartItems[] = $newCartItem;
$request->session()->put('cart', $cartItems);
$response = ['status' => 'success', 'message' => 'Item added to cart'];
return $request->ajax() ? response()->json($response) : back()->withNotify([$response]);
}
public function delete($productId)
{
$cart = Cart::where('product_id', $productId)->first();
if (!$cart) {
$status = 'error';
$message = 'Item not found';
} else {
$status = 'success';
$message = 'Item removed from cart';
$cart->delete();
}
$response = ['status' => $status, 'message' => $message];
return request()->ajax() ? response()->json($response) : back()->withNotify([$response]);
}
private function getDataForCart($product, $license = null, $isExtended = 0, $request = null)
{
$productId = $product->id;
$license = request()->license ?? Status::PERSONAL_LICENSE;
$isPersonalLicense = $license == Status::PERSONAL_LICENSE;
$existingCartItem = Cart::where('product_id', $productId);
if (auth()->check()) {
$existingCartItem->where('user_id', auth()->id());
} else {
$sessionId = $request->session()->getId();
$existingCartItem->where('session_id', $sessionId);
}
$existingCartItem = $existingCartItem->first();
if ($existingCartItem) {
return false;
}
$cart = new Cart();
$cart->product_id = $product->id;
$cart->title = $product->title;
$cart->category_id = $product->category_id;
$cart->category = @$product->category->name;
$cart->license = $license;
$cart->is_extended = $isExtended ?? 0;
$cart->extended_amount = $cart->is_extended ? gs()->twelve_month_extended_fee : 0;
$cart->price = $isPersonalLicense ? $product->price : $product->price_cl;
$cart->buyer_fee = $isPersonalLicense ? gs()->personal_buyer_fee : gs()->commercial_buyer_fee;
$cart->quantity = 1;
$cart->user_id = auth()->check() ? auth()->id() : null;
$cart->session_id = request()->session()->getId();
$cart->save();
return $cart->toArray();
}
public function collectionToCart($collectionId)
{
$collection = ProductCollection::findOrFail($collectionId);
$products = $collection->products;
$cartItems = request()->session()->get('cart', []);
foreach ($products as $product) {
$newCartItem = $this->getDataForCart($product);
$cartItems[] = $newCartItem;
request()->session()->put('cart', $cartItems);
}
$notify[] = ['success', 'Items added to cart'];
return back()->withNotify($notify);
}
public function toggleExtended($id)
{
$cart = Cart::find($id);
if (!$cart) {
return response()->json(['status' => 'error', 'message' => 'Item not found']);
}
$cart->is_extended = !$cart->is_extended;
$cart->extended_amount = $cart->is_extended ? gs()->twelve_month_extended_fee : 0;
$cart->save();
return response()->json(['status' => 'success', 'message' => 'Cart item updated']);
}
}
|