Last active
December 30, 2021 12:36
-
-
Save umutyerebakmaz/7bf4ef2406162b2b70accc347f15e195 to your computer and use it in GitHub Desktop.
Laravel 8 Advanced Shopping Cart, Sub Total, Grand Total, Calculate Dynamic Freight Rate Price, Add , Remove, Clear, Remove Selected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\Models\FreightRate; | |
use App\Models\ShoppingCart; | |
use App\Models\ShoppingSession; | |
use Illuminate\Http\JsonResponse; | |
use Illuminate\Http\RedirectResponse; | |
use Illuminate\Http\Request; | |
use App\Classes\Page; | |
use App\Models\Product; | |
use Illuminate\Support\Facades\Auth; | |
use Illuminate\View\View; | |
class ShoppingCartController extends Controller | |
{ | |
// add product to cart | |
public function add(Request $request): JsonResponse | RedirectResponse | |
{ | |
$user = $request->user(); | |
$product = Product::find($request->input('product_id')); | |
if ($user->shoppingSession === null) { | |
$freightRatePrice = FreightRate::where('rate', '=', $product->freight_rate)->pluck('price')->first(); | |
// Starting new shopping session | |
$shoppingSession = $user | |
->shoppingSession() | |
->create([ | |
'sub_total' => $product->price * $request->input('quantity'), | |
'grand_total' => ($product->price * $request->input('quantity')) + $freightRatePrice, | |
'shipping_pay' => $freightRatePrice * $request->input('quantity') | |
]); | |
// Add first product to shopping session | |
$shoppingSession | |
->shoppingCart() | |
->create([ | |
'product_id' => $request->input('product_id'), | |
'quantity' => $request->input('quantity'), | |
'freight_rate' => $product->freight_rate | |
]); | |
} else { | |
// If you have a previous shopping session | |
$shoppingSession = $user->shoppingSession->first(); | |
$existingCartItem = ShoppingCart::where('shopping_session_id', $shoppingSession->id) | |
->where('product_id', $request->input('product_id')) | |
->first(); | |
// If this product is already in the shopping cart, increase the quantity. | |
if ($existingCartItem !== null) { | |
$existingCartItem->update([ | |
'quantity' => $existingCartItem->quantity + $request->input('quantity'), | |
]); | |
} else { | |
// If the product is added to the cart for the first time | |
$shoppingSession | |
->shoppingCart() | |
->create([ | |
'product_id' => $request->input('product_id'), | |
'quantity' => $request->input('quantity'), | |
'freight_rate' => $product->freight_rate | |
]); | |
} | |
$shipping_pay = $this->calculateShippingPay($shoppingSession); | |
$shoppingSession | |
->update([ | |
'sub_total' => $shoppingSession->sub_total + ($product->price * $request->input('quantity')), | |
'grand_total' => $shoppingSession->sub_total + ($product->price * $request->input('quantity')) + $shipping_pay, | |
'shipping_pay' => $shipping_pay | |
]); | |
} | |
return response()->json([ | |
'message' => 'Ürün sepetinize eklendi.' | |
]); | |
} | |
// remove product from cart | |
public function remove(Request $request): JsonResponse | |
{ | |
$user = $request->user(); | |
$product = Product::find($request->input('product_id')); | |
$shoppingSession = $user->shoppingSession->first(); | |
$existingCartItem = ShoppingCart::where('shopping_session_id', $shoppingSession->id) | |
->where('product_id', $request->input('product_id')) | |
->first(); | |
// ürünü sil | |
if ($existingCartItem->quantity === 1) { | |
$existingCartItem->delete(); | |
} else { | |
$existingCartItem->update([ | |
'quantity' => $existingCartItem->quantity - $request->input('quantity') | |
]); | |
} | |
// sessionu sil | |
if ($shoppingSession->shoppingCart->isEmpty()) { | |
$shoppingSession->delete(); | |
} else { | |
$shipping_pay = $this->calculateShippingPay($shoppingSession); | |
$shoppingSession->update([ | |
'sub_total' => $shoppingSession->sub_total - ($product->price * $request->input('quantity')), | |
'grand_total' => $shoppingSession->sub_total - ($product->price * $request->input('quantity')) + $shipping_pay, | |
'shipping_pay' => $shipping_pay | |
]); | |
} | |
return response()->json([ | |
'message' => 'Ürün sepetinizden çıkarıldı.' | |
]); | |
} | |
// calculator | |
public function calculateShippingPay(ShoppingSession $shoppingSession): float | |
{ | |
$rateSum = 0; | |
foreach ($shoppingSession->shoppingCart as $item) { | |
$rateSum += ($item->quantity * $item->freight_rate); | |
} | |
if ($rateSum >= 30) { | |
return 0; | |
} else { | |
$shippingPay = FreightRate::where('rate', $rateSum)->first(); | |
} | |
return $shippingPay->price; | |
} | |
// remove all selected product in cart | |
public function removeProduct(Request $request): JsonResponse | |
{ | |
$user = $request->user(); | |
$product = Product::find($request->input('product_id')); | |
$shoppingSession = $user->shoppingSession->first(); | |
$existingCartItem = ShoppingCart::where('shopping_session_id', $shoppingSession->id) | |
->where('product_id', $request->input('product_id')) | |
->first(); | |
if ($shoppingSession->shoppingCart->isEmpty()) { | |
$shoppingSession->delete(); | |
} else { | |
$shipping_pay = $this->calculateShippingPay($shoppingSession); | |
$shoppingSession->update([ | |
'sub_total' => $shoppingSession->sub_total - ($product->price * $existingCartItem->quantity), | |
'grand_total' => $shoppingSession->sub_total - ($product->price * $existingCartItem->quantity) + $shipping_pay, | |
'shipping_pay' => $shipping_pay | |
]); | |
} | |
$existingCartItem->delete(); | |
return response()->json([ | |
'message' => 'Ürün sepetinizden çıkarıldı.' | |
]); | |
} | |
// empty shopping cart, delete shopping session. | |
public function clear(Request $request): JsonResponse | |
{ | |
$user = $request->user(); | |
$user->shoppingSession() | |
->first() | |
->delete(); | |
return response()->json([ | |
'message' => 'Sepetiniz temizlendi.' | |
]); | |
} | |
// page: shopping cart | |
public function sepetim(): View | |
{ | |
$page = new Page; | |
$page->title = 'Sepetim'; | |
$page->description = 'Sepetim'; | |
$shoppingSession = Auth::user()->shoppingSession; | |
return view('pages.shopping-cart', compact('page', 'shoppingSession')); | |
} | |
} |
Author
umutyerebakmaz
commented
Dec 30, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment