D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
forge
/
ghanempharmacy.com
/
app
/
Http
/
Controllers
/
Web
/
Filename :
ProductBundleController.php
back
Copy
<?php namespace App\Http\Controllers\Web; use App\Http\Controllers\Controller; use App\Models\ProductBundle; use App\Models\Cart; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; class ProductBundleController extends Controller { /** * Display all active bundles */ public function index() { $bundles = ProductBundle::active() ->with('bundleItems.product') ->orderBy('priority', 'desc') ->orderBy('created_at', 'desc') ->paginate(12); return view(VIEW_FILE_NAMES['bundles'], compact('bundles')); } /** * Display bundle details */ public function show($slug) { $bundle = ProductBundle::where('slug', $slug) ->with('bundleItems.product') ->firstOrFail(); // Check if bundle is active if (!$bundle->isActive()) { abort(404); } return view(VIEW_FILE_NAMES['bundle_details'], compact('bundle')); } /** * Add bundle to cart */ public function addToCart(Request $request) { $request->validate([ 'bundle_id' => 'required|exists:product_bundles,id' ]); $bundle = ProductBundle::with('bundleItems.product')->findOrFail($request->bundle_id); // Check if bundle is active if (!$bundle->isActive()) { return response()->json([ 'status' => 'error', 'message' => translate('bundle_is_not_active') ]); } // Check if all products are available foreach ($bundle->bundleItems as $item) { if (!$item->product) { return response()->json([ 'status' => 'error', 'message' => translate('some_products_are_not_available') ]); } // Check stock if ($item->product->current_stock < $item->quantity) { return response()->json([ 'status' => 'error', 'message' => translate('insufficient_stock_for') . ' ' . $item->product->name ]); } } try { // Get user ID or guest session $userId = Auth::guard('customer')->id(); $guestId = session()->get('guest_id'); if (!$userId && !$guestId) { $guestId = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 9); session()->put('guest_id', $guestId); } // Add each product in the bundle to cart foreach ($bundle->bundleItems as $item) { $cartItem = Cart::where('product_id', $item->product_id); if ($userId) { $cartItem->where('customer_id', $userId); } else { $cartItem->where('guest_id', $guestId); } $existingCart = $cartItem->first(); if ($existingCart) { // Update quantity $existingCart->quantity += $item->quantity; $existingCart->save(); } else { // Create new cart item Cart::create([ 'customer_id' => $userId, 'guest_id' => $guestId, 'product_id' => $item->product_id, 'product_type' => 'physical', 'color' => null, 'choices' => json_encode([]), 'variations' => json_encode([]), 'variant' => null, 'quantity' => $item->quantity, 'price' => $item->product->unit_price, 'tax' => \App\Utils\Helpers::get_tax_amount($item->product), 'discount' => \App\Utils\Helpers::get_product_discount($item->product, $item->product->unit_price), 'slug' => $item->product->slug, 'name' => $item->product->name, 'thumbnail' => $item->product->thumbnail, 'seller_id' => $item->product->user_id, 'seller_is' => $item->product->added_by, 'shipping_cost' => 0, 'shipping_type' => null, 'bundle_id' => $bundle->id, 'bundle_name' => $bundle->title, ]); } } return response()->json([ 'status' => 'success', 'message' => translate('bundle_added_to_cart_successfully') ]); } catch (\Exception $e) { return response()->json([ 'status' => 'error', 'message' => translate('error_adding_to_cart') ]); } } /** * Get active bundles for homepage or other sections */ public function getActiveBundles($limit = 6) { return ProductBundle::active() ->with('bundleItems.product') ->orderBy('priority', 'desc') ->orderBy('created_at', 'desc') ->limit($limit) ->get(); } }