'required', 'metalType' => 'required', 'metalColor' => 'required', 'diamond_type' => 'required' ]; $messages = [ 'product_sku.required' => 'Product Sku is required.', 'metalType.required' => 'Metal Type is required.', 'metalColor.required' => 'Metal Color is required.', 'diamond_type.required' => 'Diamond Type is required.', ]; $validator = Validator::make($request->all(), $rules, $messages); if ($validator->fails()) { $errors = $validator->errors()->all(); $output['res'] = 'error'; $output['msg'] = $errors; } try { if ($request->metalColor == 'rose' || $request->metalColor == 'Rose') { $metalColor = "pink"; } else { $metalColor = $request->metalColor; } $product_price = ProductPrice::where('product_sku', $request->product_sku) ->where('metalType', $request->metalType) ->where('metalColor', $metalColor) ->where('diamond_type', $request->diamond_type) ->first(); if ($product_price) { $output['res'] = 'success'; $output['msg'] = 'product price is :'; $output['data'] = ['price' => round($product_price->price, 0), 'diamond_type' => $product_price->diamond_type]; } else { $output['res'] = 'error'; $output['msg'] = 'product price not found'; $output['data'] = ''; } return response()->json($output, 200); } catch (Exception $e) { if ($product_price) { $output['res'] = 'error'; $output['msg'] = 'product price is :'; $output['data'] = $e->getMessage(); } return response()->json($output, 500); } } public function index(Request $request) { ##################################################### $output['res'] = 'success'; $output['msg'] = 'data retrieved successfully'; // Initialize the query with the base conditions and join with product_price table // $products = ProductModel::query() // ->leftJoin('product_price', 'products.sku', '=', 'product_price.product_sku') // ->where('products.status', 'true') // ->where('products.menu', 7) // ->whereNull('products.parent_sku') // ->where('product_price.diamond_type', 'natural') // Filter for natural diamond_type // ->where('product_price.metalColor', 'White') // ->where('product_price.metalType', '18kt') // ->select('products.*','product_price.price') // ## ->orderByRaw("CAST(product_price.price AS DECIMAL(12, 4)) ASC") // ->distinct(); // Ensure distinct products ####### new script start ####### $products = ProductModel::query() ->leftJoin('product_price', 'products.sku', '=', 'product_price.product_sku') ->where('products.menu', 7) ->where('products.status', 'true') ->whereNull('products.parent_sku') ->select('products.*', DB::raw('IFNULL(product_price.price, 0) as price')); ####### new script end ####### // Apply the bridal sets filter if ($request->bridal_sets == 'true') { $products->whereNotNull('products.matching_wedding_band'); } // Apply sorting based on request if (!is_null($request->query('sortby'))) { $sortBy = $request->query('sortby'); if ($sortBy == 'low_to_high') { // $products->orderBy('product_price.price', 'asc'); $products->orderByRaw("CAST(product_price.price AS DECIMAL(12, 4)) ASC"); } elseif ($sortBy == 'high_to_low') { // $products->orderBy('product_price.price', 'desc'); $products->orderByRaw("CAST(product_price.price AS DECIMAL(12, 4)) DESC"); } elseif ($sortBy == 'Newest') { $products->orderBy('products.created_at', 'desc'); } elseif ($sortBy == 'best_seller') { $products->where('products.is_bestseller', '1'); } } // Apply additional filters if (!is_null($request->query('shape'))) { $products->where('products.CenterShape', strtoupper(trim($request->query('shape')))); } if (!is_null($request->query('ring_style'))) { $subcatSlugs = explode(',', $request->query('ring_style')); // Fetch corresponding IDs based on slugs $subcatIds = Subcategory::whereIn('slug', $subcatSlugs)->pluck('id')->toArray(); // If there are IDs, use them in the WHERE clause if (!empty($subcatIds)) { $products->where(function ($query) use ($subcatIds) { foreach ($subcatIds as $id) { $query->orWhereRaw("FIND_IN_SET(?, products.sub_category)", [$id]); } }); } } if (!is_null($request->query('metal_color'))) { $metalcolor_id = $request->query('metal_color'); $products->where('products.metalColor_id', $metalcolor_id); } if (!is_null($request->query('price_range'))) { $range = explode(',', $request->query('price_range')); $min = $range[0]; $max = $range[1]; $products->whereBetween('product_price.price', [$min, $max]); } // Apply filters for diamond_type and metalColor $products->where('product_price.diamond_type', 'natural') // Filter for natural diamond_type ->where('product_price.metalColor', 'White'); // Filter for White metalColor // Count the filtered results $actual_count = $products->count(); // Retrieve the products if needed (for pagination or display) $productsList = $products->paginate(30); $count = $productsList->count(); if ($count) { $productList = []; foreach ($productsList as $product) { // $product->name = (!empty($product->product_browse_pg_name)) ? ucfirst(strtolower($product->product_browse_pg_name)) : ucfirst(strtolower($product->name)); $product->name = ucfirst(strtolower($product->name)); $product->description = ucfirst(strtolower($product->description)); $product->images = json_decode($product->images); $product->videos = json_decode($product->videos); $name = strtolower($product->name); $product->name = ucwords($name); // Get the prices based on different criteria $white_gold_price = ProductPrice::where('product_sku', $product['sku']) ->where('metalType', '18kt') ->where('metalColor', 'White') ->where('diamond_type', 'natural') ->first() ->price ?? 0; $product->white_gold_price = round($white_gold_price, 0); $yellow_gold_price = ProductPrice::where('product_sku', $product['sku']) ->where('metalType', '18kt') ->where('metalColor', 'Yellow') ->where('diamond_type', 'natural') ->first() ->price ?? 0; $product->yellow_gold_price = round($yellow_gold_price, 0); $rose_gold_price = ProductPrice::where('product_sku', $product['sku']) ->where('metalType', '18kt') ->where('metalColor', 'Pink') ->where('diamond_type', 'natural') ->first() ->price ?? 0; $product->rose_gold_price = round($rose_gold_price, 0); $platinum_price = ProductPrice::where('product_sku', $product['sku']) ->where('metalType', 'Platinum') ->where('metalColor', 'White') ->where('diamond_type', 'natural') ->first() ->price ?? 0; $product->platinum_price = round($platinum_price, 0); array_push($productList, $product); } // Attach the results to the output $output['data'] = $productList; $output['count'] = $count; $output['product_count'] = $actual_count; } else { $output['res'] = 'error'; $output['msg'] = 'No product found!'; $output['data'] = []; return response()->json($output, 200); } return response()->json($output); ###################################################### return response()->json($output, 200); } public function productDetails($entity_id) { $output['res'] = 'success'; $output['msg'] = 'data retrieved successfully'; if (!is_null($entity_id)) { $product = ProductModel::where('entity_id', $entity_id)->orWhere('slug', $entity_id)->first(); $product['name'] = ucfirst(strtolower(!empty($product['name']) ? $product['name'] : $product['product_browse_pg_name'])); $product['description'] = ucfirst(strtolower($product['description'])); ## fetch images // $pro_images = ProductImageModel::where('product_id', $product['id'])->get(); // count($pro_images); // $images_arr = []; // if (count($pro_images) > 0) { // foreach ($pro_images as $product_img) { // // var_dump($product_img); // $pimg = env('AWS_URL') . 'products/images/' . $product['internal_sku'] . '/' . $product_img['image_path']; // $images_arr[] = $pimg; // } // } else { // $images_arr = json_decode($product['images']); // } // var_dump($images_arr); // $product['images'] = $images_arr; $product['images'] = json_decode($product['images']); $product['videos'] = json_decode($product['videos']); $priceData = ProductPrice::where('product_sku', $product['sku'])->where('metalType', '18kt')->where('metalColor', 'White')->where('diamond_type', 'natural')->first(); $product['white_gold_price'] = round($priceData['price'] ?? 0, 0); $product['yellow_gold_price'] = round(ProductPrice::where('product_sku', $product['sku'])->where('metalType', '18kt')->where('metalColor', 'Yellow')->where('diamond_type', 'natural')->first()['price'] ?? 0, 0); $product['rose_gold_price'] = round(ProductPrice::where('product_sku', $product['sku'])->where('metalType', '18kt')->where('metalColor', 'Pink')->where('diamond_type', 'natural')->first()['price'] ?? 0, 0); $product['platinum_price'] = round(ProductPrice::where('product_sku', $product['sku'])->where('metalType', 'Platinum')->where('metalColor', 'White')->where('diamond_type', 'natural')->first()['price'] ?? 0, 0); $product['diamond_type'] = 'natural'; $product['diamondQuality'] = $priceData['diamondQuality'] ?? 0; $product['metalType'] = '18KT Gold'; $center_stone_options = explode('/', $product['center_stone_options']); if (in_array('Princess', $center_stone_options)) { $center_stone_options = array_diff($center_stone_options, ['Princess']); } $product['center_stone_options'] = implode('/', $center_stone_options); // $product['center_stone_options'] = $product['center_stone_options']; if (!is_null($product['matching_wedding_band']) || !empty($product['matching_wedding_band'])) { ## if matching set exist then reterive tha details and send them $is_matchingset = ProductModel::where('sku', $product['matching_wedding_band']); if ($is_matchingset->exists()) { $matching_bands_product = $is_matchingset->first(); #####IMAGES MATCHING BANDS###### // $matching_images = ProductImageModel::where('product_id', $matching_bands_product->id)->get(); // $matching_images_arr = []; // if (count($matching_images) > 0) { // foreach ($matching_images as $mproduct_img) { // $mimg = env('AWS_URL') . 'products/images' . $mproduct_img->internal_sku . '/' . $mproduct_img->image_path; // $matching_images_arr[] = $mimg; // } // } else { // $matching_images_arr = json_decode($matching_images->images); // } #####IMAGES MATCHING BANDS###### // $matching_bands_product->images = $matching_bands_product; // $matching_bands_product->images = $matching_images_arr; $matching_bands_product->price = round(ProductPrice::where('product_sku', $matching_bands_product['sku'])->where('metalType', '18kt')->where('metalColor', 'White')->where('diamond_type', 'natural')->first()['price'] ?? 0, 0); $product['matching_wedding_band'] = $matching_bands_product; } else { $product['matching_wedding_band'] = NULL; } } ## check if center stone exist then get the value $cenyterStoneArr = []; $centerStone = explode(',', $product->center_stones); if (!is_null($centerStone)) { foreach ($centerStone as $index => $s_stone) { $s_values = CenterStone::find($s_stone); $cenyterStoneArr[] = $s_values; } } $product['center_stones'] = $cenyterStoneArr; if (!is_null($product['similar_products'])) { $product['similar_products'] = json_encode($this->getSimilarProducts($product['similar_products'])); } if ($product['parent_sku'] != NULL) { $var = ProductModel::where('parent_sku', $product['parent_sku'])->get(); if ($var->isNotEmpty()) { $collect_fractionsemimount = []; $vardata = []; foreach ($var as $variant) { if (!in_array($variant['fractionsemimount'], $collect_fractionsemimount)) { //check if current date is in the already_echoed array $variantData = [ 'name' => $variant['name'], 'id' => $variant['id'], 'slug' => $variant['slug'], 'sku' => $variant['fractionsemimount'] ]; $vardata[] = $variantData; } $collect_fractionsemimount[] = $variant['fractionsemimount']; } } $product['variants'] = $vardata; } else if ($product['parent_sku'] == NULL || empty($product['parent_sku'])) { $var = ProductModel::where('parent_sku', $product['sku'])->get(); if ($var->isNotEmpty()) { $collect_fractionsemimount = []; $vardata = []; foreach ($var as $variant) { if (!in_array($variant['fractionsemimount'], $collect_fractionsemimount)) { //check if current date is in the already_echoed array $variantData = [ 'name' => ucfirst(strtolower(!empty($variant['product_browse_pg_name']) ? $variant['product_browse_pg_name'] : $variant['name'])), 'id' => $variant['id'], 'slug' => $variant['slug'], 'sku' => $variant['fractionsemimount'] ]; $vardata[] = $variantData; } $collect_fractionsemimount[] = $variant['fractionsemimount']; } } else { $vardata = []; } $product['variants'] = $vardata; } } $output['from'] = 'db'; $output['data'] = $product; return response()->json($output, 200); } public function searhSuggestion(Request $request) { $output['res'] = 'success'; $output['msg'] = 'suggestions are ...'; $q = $request->input('q'); if (!empty($q)) { $products = ProductModel::orderBy('entity_id', 'desc') ->where('status', 'true') ->where(function ($query) use ($q) { $query->where('name', 'like', "$q%") ->orWhere('sku', 'like', "$q%") ->orWhere('metalColor', 'like', "$q%") ->orWhere('diamondQuality', 'like', "$q%") ->orWhere('diamondQuality', 'like', "$q%") ->orWhere('CenterShape', 'like', "$q%") ->orWhere('white_gold_price', 'like', "$q%") ->orWhere('yellow_gold_price', 'like', "$q%") ->orWhere('rose_gold_price', 'like', "$q%") ->orWhere('fractioncomplete', 'like', "$q%") ->orWhere('metalType', 'like', "$q%") ->orWhere('metalWeight', 'like', "$q%") ->orWhere('finishLevel', 'like', "$q%"); }) ->select('name', 'product_browse_pg_name', 'fractionsemimount', 'slug', 'menu', 'default_image_url', 'white_gold_price', 'sku') ->limit(5) ->get(); $searched_product = []; foreach ($products as $product) { $product->description = ucfirst(strtolower($product->description)); $product->images = json_decode($product->images); $product->videos = json_decode($product->videos); $name = strtolower($product->product_browse_pg_name); $product->name = ucfirst($name); $product->menu = Menu::find($product->menu)['slug']; $product->white_gold_price = round(ProductPrice::where('product_sku', $product['sku'])->where('metalType', '18kt')->where('metalColor', 'White')->where('diamond_type', 'natural')->first()['price'] ?? 0, 0); array_push($searched_product, $product); } } else { $searched_product = []; } $output['data'] = $searched_product; return response()->json($output, 200); } public function globleSearch(Request $request) { $output['res'] = 'success'; $output['msg'] = 'data retrieved successfully'; $q = $request->input('q'); if (!empty($q)) { $products = ProductModel::orderBy('entity_id', 'desc') ->where('status', 'true') ->where(function ($query) use ($q) { $query->where('name', 'like', "$q%") ->orWhere('sku', 'like', "$q%") ->orWhere('metalColor', 'like', "$q%") ->orWhere('diamondQuality', 'like', "$q%") ->orWhere('diamondQuality', 'like', "$q%") ->orWhere('CenterShape', 'like', "$q%") ->orWhere('white_gold_price', 'like', "$q%") ->orWhere('yellow_gold_price', 'like', "$q%") ->orWhere('rose_gold_price', 'like', "$q%") ->orWhere('fractioncomplete', 'like', "$q%") ->orWhere('metalType', 'like', "$q%") ->orWhere('metalWeight', 'like', "$q%") ->orWhere('finishLevel', 'like', "$q%"); }); if (!is_null($request->query('shape'))) { $shapes = explode(',', strtoupper(trim($request->query('shape')))); $products->whereIn('CenterShape', $shapes); } if (!is_null($request->query('ring_style'))) { $subcatSlugs = explode(',', $request->query('ring_style')); // Fetch corresponding IDs based on slugs $subcatIds = Subcategory::whereIn('slug', $subcatSlugs)->pluck('id')->toArray(); // If there are IDs, use them in the WHERE clause if (!empty($subcatIds)) { $products->where(function ($query) use ($subcatIds) { foreach ($subcatIds as $id) { $query->orWhereRaw("FIND_IN_SET(?, sub_category)", [$id]); } }); } } if (!is_null($request->query('metal_color'))) { $metalcolor_ids = explode(',', $request->query('metal_color')); $products->whereIn('metalColor', $metalcolor_ids); } $count = $products->count(); $products = $products->paginate(30); $searched_product = []; foreach ($products as $product) { // $product->name = ucfirst(strtolower($product->name)); $product->description = ucfirst(strtolower($product->description)); $product->images = json_decode($product->images); $product->videos = json_decode($product->videos); $name = strtolower($product->product_browse_pg_name); // $product->name = ucwords($name); $product->name = ucfirst($name . ' ' . $product->fractionsemimount); $product->menu = Menu::find($product->menu)['slug']; $product->white_gold_price = round(ProductPrice::where('product_sku', $product['sku'])->where('metalType', '18kt')->where('metalColor', 'White')->where('diamond_type', 'natural')->first()['price'] ?? 0, 0); array_push($searched_product, $product); } } else { $searched_product = []; } $output['product_count'] = isset($count) ? $count : 0; $output['data'] = $searched_product; return response()->json($output, 200); } public function productStyle(Request $request) { $output['res'] = 'success'; $output['msg'] = 'data retrieved successfully'; $cacheKey = 'ring_style'; $ring_style = Cache::get($cacheKey); if (!$ring_style) { $ring_styles = Subcategory::orderBy('order_number', 'asc')->where('menu_id', 7)->where('category_id', 7)->where('status', 'true')->get(); foreach ($ring_styles as $val) { if (!empty($val['image'])) { // $val['image'] = env('AWS_URL').'public/storage/' . $val->image; $val['image'] = env('AWS_URL') . 'public/' . $val->image; } } Cache::put($cacheKey, $ring_styles, $minutes = 60); $output['from'] = 'db'; $output['data'] = $ring_styles; return response()->json($output, 200); } else { $output['from'] = 'cache'; $output['data'] = $ring_style; return response()->json($output, 200); } } public function getSimilarProducts($ids) { $products = []; $ids = explode(',', $ids); foreach ($ids as $product_id) { $pro = ProductModel::where('id', $product_id)->first(); if ($pro) { $products[] = $pro; } } return $products; } ## coveted Products public function covetedProducts($menu_name) { $output['res'] = 'success'; $output['msg'] = 'data retrieved successfully'; if ($menu_name == 'engagement-rings') { $menu = 7; $query = ProductModel::where('menu', $menu) ->where('status', 'true') ->orwhere('is_newest', 1) ->orwhere('is_bestseller', 1) ->limit(5) ->get(); } else { $menu = 2; $query = ProductModel::where('menu', $menu) ->where('status', 'true') ->where('is_newest', 1) ->orwhere('is_bestseller', 1) ->limit(5) ->get(); } $output['data'] = $query; return response()->json($output, 200); } }