Advertisement
backlight0815

Cart Controller

Jul 21st, 2023
1,293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.94 KB | Source Code | 0 0
  1. public function checkout(Request $request)
  2.     {
  3.  
  4.         // Get the authenticated user ID
  5.         $userId = Auth::id();
  6.         $cartItems = Cart::where('user_id', $userId)->get();
  7.  
  8.         // Get the total amount from the form submission
  9.         $totalAmount = $request->input('total_amount');
  10.  
  11.             // Get the cart items for the authenticated user
  12.  
  13.         $cartItems = Cart::where('user_id', $userId)->get();
  14.  
  15.           // Check if there are any cart items
  16.     if (!$cartItems) {
  17.         // Handle the case when there are no cart items
  18.         // You may redirect back with an error message or perform other actions
  19.         return redirect()->back()->with('error', 'Your cart is empty.');
  20.     }
  21.  
  22.         // Step 1: Create an order
  23.         $order = new orders();
  24.         $order->user_id = $userId;
  25.         $order->total_amount = $totalAmount;
  26.         $order->save();
  27.  
  28.  // Step 2: Create order items
  29.  foreach ($cartItems as $cartItem) {
  30.     $orderItem = new order_items();
  31.     $orderItem->product_id = $cartItem->product_id;
  32.     $orderItem->order_id = $order->id;
  33.     $orderItem->user_id = $userId;
  34.     $orderItem->quantity = $cartItem->quantity;
  35.     $orderItem->save();
  36.  
  37.        // Deduct product_stock for the ordered product
  38.        $product = Product::find($cartItem->product_id);
  39.        if ($product) {
  40.            $product->product_stock -= $cartItem->quantity;
  41.            $product->save();
  42.        }
  43. }
  44.  
  45.         // Step 3: Store the transaction (payment receipt)
  46.         $transaction = new transactions();
  47.         $transaction->order_id = $order->id;
  48.         $transaction->user_id = $userId;
  49.  
  50.         $image = $request->file('receipt');
  51.         $imageName = $image->getClientOriginalName();
  52.         $image->move(public_path('upload/payment_proof/'),$imageName);
  53.  
  54.         $transaction->payment_proof = $imageName;
  55.  
  56.         // Image upload logic
  57.  
  58.  
  59. // if ($request->hasFile('receipt')) {
  60. //     $image = $request->file('receipt');
  61. //     $name_gen = hexdec(uniqid()) . '.' . $image->getClientOriginalExtension();
  62. //     Image::make($image)->resize(430, 327)->save('upload/payment_proof/' . $name_gen);
  63. //     $save_url = 'upload/payment_proof/' . $name_gen;
  64. //     $transaction->payment_proof = $save_url;
  65. // }
  66.         // Handle the receipt file upload and store its path in the 'payment_proof' field.
  67.         // Example: $transaction->payment_proof = $request->file('receipt')->store('receipts');
  68.  
  69.           // Image upload logic
  70.  
  71.         $transaction->save();
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.         // Clear the cart or perform any other required actions.
  80.  // Clear the cart items
  81.  Cart::where('user_id', $userId)->delete();
  82.         // Redirect to a success page after successful checkout
  83.         $notification = array(
  84.             'message' => 'Make payment successfully',
  85.             'alert-type' => 'success'
  86.         );
  87.  
  88.         // Proceed with any other necessary steps
  89.  
  90.         return redirect()->back()->with($notification);
  91.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement