Viewing file: CartRepository.php (30.48 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Repositories\Front;
use App\{ Models\Cart, Models\Item, Models\PromoCode, Helpers\PriceHelper }; use App\Models\AttributeOption; use App\Models\Attribute; use Illuminate\Support\Facades\Session;
class CartRepository {
/** * Store cart. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */
public function store($request) {
// dd($request->all()); $user = auth()->user()->id; $cartKey = 'cart_' . $user; $cart = Session::get($cartKey, []); $input = $request->all(); $itemId = $input['item_id']; $qty = isset($input['quantity']) ? max(1, (int) $input['quantity']) : 1;
// Fetch the product from the database $item = Item::where('id', $itemId) ->select('id', 'name', 'photo', 'discount_price', 'stock', 'slug', 'item_type') ->firstOrFail();
$attributes = []; $attributePrice = 0; $optionIds = [];
// Handle attributes (like size, color) if (isset($input['options_ids'])) { $options = is_array($input['options_ids']) ? $input['options_ids'] : explode(',', $input['options_ids']);
foreach ($options as $optionId) { $option = AttributeOption::findOrFail($optionId); if ($qty > $option->stock) { return response()->json(['status' => 'outStock', 'message' => 'Product Out Of Stock']); }
$attributes[] = ['name' => $option->name, 'price' => $option->price]; $attributePrice += $option->price; $optionIds[] = $option->id; } }
// Create a unique key based on product and attributes $attributeHash = md5(serialize($attributes)); $uniqueCartKey = $itemId . '_' . $attributeHash;
// If the item with same attributes exists, increase the quantity if (isset($cart[$uniqueCartKey])) { $cart[$uniqueCartKey]['qty'] += $qty; } else { // Add a new item to the cart $cart[$uniqueCartKey] = [ 'options_id' => $optionIds, 'attribute' => $attributes, 'attribute_price' => $attributePrice, 'name' => $item->name, 'slug' => $item->slug, 'qty' => $qty, 'price' => PriceHelper::grandPrice($item), 'main_price' => $item->discount_price, 'photo' => $item->photo, 'item_type' => $item->item_type, 'item_id' => $item->id ]; }
// Save the cart back to the session Session::put($cartKey, $cart);
return response()->json([ 'status' => 'success', 'message' => __('Cart Created successfully'), 'qty' => count($cart), 'cart' => $cart, ]); }
public function update_cart_qty($request) { $g_total = 0; $user = auth()->user()->id; $cartKey = 'cart_' . $user; $cart = Session::get($cartKey, []); $uniqueCartKey = $request->item_key; if (isset($cart[$uniqueCartKey])) { $item_id = $cart[$uniqueCartKey]['item_id']; $product_item = Item::where('id', $item_id) ->select('id', 'name', 'photo', 'discount_price', 'stock', 'slug', 'item_type') ->firstOrFail(); if ($request->data_action === 'increment') { if ($product_item->stock <= 0) { $g_total = $this->calculateGrandTotal($cart); return response()->json([ 'status' => 'failed', 'stock' => $product_item->stock, 'item_qty' => $cart[$uniqueCartKey]['qty'], 'grand_total' => $g_total, 'message' => __('Product is out of stock'), ]); } if ($cart[$uniqueCartKey]['qty'] + 1 > $product_item->stock) { $g_total = $this->calculateGrandTotal($cart); return response()->json([ 'status' => 'failed', 'stock' => $product_item->stock, 'item_qty' => $cart[$uniqueCartKey]['qty'], 'grand_total' => $g_total, 'message' => __('Cannot add more than available stock'), ]); } $cart[$uniqueCartKey]['qty']++; } elseif ($request->data_action === 'decrement' && $cart[$uniqueCartKey]['qty'] > 1) { $cart[$uniqueCartKey]['qty']--; } $main_price = $cart[$uniqueCartKey]['main_price']; $attribute_price = $cart[$uniqueCartKey]['attribute_price']; $item_main_price = $cart[$uniqueCartKey]['qty'] * ($main_price + $attribute_price); Session::put($cartKey, $cart); $g_total = $this->calculateGrandTotal($cart); return response()->json([ 'status' => 'success', 'message' => __('Cart quantity updated successfully'), 'price' => $item_main_price, 'grand_total' => $g_total, 'stock' => $product_item->stock, 'item_qty' => $cart[$uniqueCartKey]['qty'], ]); } else { return response()->json([ 'status' => 'failed', 'message' => __('Item not found in the cart'), ]); } } private function calculateGrandTotal($cart) { $g_total = 0; foreach ($cart as $item) { $g_total += $item['qty'] * ($item['main_price'] + $item['attribute_price']); } return $g_total; }
// public function store($request) // {
// // dd($request->all()); // $user = auth()->user()->id; // $qty_check = 0; // $input = $request->all(); // $input['option_name'] = []; // $input['option_price'] = []; // $input['attr_name'] = []; // $attributes = []; // $attribute_price = 0; // $qty = isset($input['quantity']) ? (is_numeric($input['quantity']) ? $input['quantity'] : 1) : 1; // // Process options_ids // if (isset($input['options_ids'])) { // $options_ids = is_array($input['options_ids']) ? implode(',', $input['options_ids']) : $input['options_ids'];
// if ($options_ids) { // foreach (explode(',', $options_ids) as $optionId) { // $option = AttributeOption::findOrFail($optionId); // if ($qty > $option->stock) { // return ['message' => 'Product Out Of Stock', 'status' => 'outStock']; // }
// $attributes[] = [ // 'name' => $option->name, // 'price' => $option->price // ]; // $attribute_price += $option->price; // } // } // }
// $cartKey = 'cart_' . $user; // $cart = Session::get($cartKey, []); // // dd($cart,$input); // $item = Item::where('id', $input['item_id']) // ->select('id', 'name', 'photo', 'discount_price', 'previous_price', 'slug', 'item_type', 'license_name', 'license_key', 'stock') // ->firstOrFail();
// if ($item->item_type == 'normal' && $item->stock < $qty) { // return ['message' => 'Product Out Of Stock', 'status' => 'outStock']; // }
// $option_id = []; // if (isset($request->type) && $request->type == '1') { // foreach ($item->attributes as $attr) { // if (isset($attr->options[0]->name)) { // $option_id[] = $attr->options[0]->id; // } // }
// $input['attr_name'] = array_column($attributes, 'name'); // $input['option_price'] = array_column($attributes, 'price'); // $input['option_name'] = array_column($attributes, 'name'); // }
// // Check if item is already in the cart // if (!isset($cart[$item->id])) { // $license_name = json_decode($item->license_name, true); // $license_key = json_decode($item->license_key, true); // $cart[$item->id] = [ // 'options_id' => $option_id, // 'attribute' => $attributes, // 'attribute_price' => $attribute_price, // 'name' => $item->name, // 'slug' => $item->slug, // 'qty' => $qty, // 'price' => PriceHelper::grandPrice($item), // 'main_price' => $item->discount_price, // 'photo' => $item->photo, // 'cart_item_id' => $item->id, // 'item_type' => $item->item_type, // 'item_l_n' => $item->item_type == 'license' ? end($license_name) : null, // 'item_l_k' => $item->item_type == 'license' ? end($license_key) : null, // ];
// Session::put($cartKey, $cart); // return ['message' => __('Product added successfully'), 'qty' => count(Session::get($cartKey))]; // }
// if (isset($cart[$item->id])) { // dd($cart,'ppppp'); // $existingAttributes = $request['item_key']; // // dd($cart[$item->id]['attribute'], 'oppdpdpd', $attributes,$option_id); // if ($existingAttributes !== $attributes) { // $cart_item_id = uniqid(); // $cart[$item->id . '-' . $cart_item_id] = [ // 'options_id' => $option_id, // 'attribute' => $attributes, // 'attribute_price' => $attribute_price, // 'name' => $item->name, // 'slug' => $item->slug, // 'qty' => $qty, // 'price' => PriceHelper::grandPrice($item), // 'main_price' => $item->discount_price, // 'photo' => $item->photo, // 'cart_item_id' => $item->id . '-' . $cart_item_id, // 'item_type' => $item->item_type, // ]; // } else { // $cart[$item->id]['qty'] += $qty; // }
// if ($item->item_type == 'normal' && $item->stock <= (int) $cart[$item->id]['qty']) { // return ['message' => 'Product Out Of Stock', 'status' => 'outStock']; // }
// Session::put($cartKey, $cart); // return ['message' => __('Product added successfully'), 'qty' => count(Session::get($cartKey))]; // }
// return ['message' => __('Product added successfully'), 'qty' => count(Session::get($cartKey))]; // } // $user = auth()->user()->id; // $qty_check = 0; // dd($request->all()); // $input = $request->all(); // $input['option_name'] = []; // $input['option_price'] = []; // $input['attr_name'] = []; // $attributes = []; // $attribute_price = 0; // $qty = isset($input['quantity']) ? $input['quantity'] : 1; // $qty = is_numeric($qty) ? $qty : 1; // if ((isset($input['options_ids']))) {
// $options_ids = is_array($input['options_ids']) // ? implode(',', $input['options_ids']) // : $input['options_ids']; // if($options_ids){ // foreach (explode(',', $options_ids) as $optionId) { // $option = AttributeOption::findOrFail($optionId); // if ($qty > $option->stock) { // $data = ['message' => 'Product Out Of Stock', 'status' => 'outStock']; // return $data; // } // $attributes[] = [ // 'name' => $option->name, // 'price' => $option->price // ]; // $attribute_price += $option->price;
// } // $attribute_price = $attributes['price']; // }
// } // dd($attributes,$attribute_price);
// $cartKey = 'cart_' . $user; // $cart = Session::get($cartKey); // dd($cart); // $item = Item::where('id', $input['item_id']) // ->select('id', 'name', 'photo', 'discount_price', 'previous_price', 'slug', 'item_type', 'license_name', 'license_key', 'stock') // ->firstOrFail();
// if ($item->item_type == 'normal' && $item->stock < $qty) { // $data = ['message' => 'Product Out Of Stock', 'status' => 'outStock']; // return $data; // }
// if ($item->item_type == 'digital' || $item->item_type == 'license') { // $attribute_price = 0; // if (Session::has($cartKey)) { // dd($cartKey); // dd('entered into'); // if (array_key_exists($input['item_id'], $cart)) { // $data = ['message' => 'Product already added', 'status' => 'alreadyInCart']; // return $data; // } // } // }
// $option_id = []; // if (isset($request->type) && $request->type == '1') {
// $attr_name = []; // $option_name = []; // $option_price = [];
// foreach ($item->attributes as $attr) { // if (isset($attr->options[0]->name)) { // $attr_name[] = $attr->name; // $option_name[] = $attr->options[0]->name; // $option_price[] = $attr->options[0]->price; // $option_id[] = $attr->options[0]->id; // } // }
// $input['attr_name'] = $attr_name; // $input['option_price'] = $option_price; // $input['option_name'] = $option_name; // $input['option_id'] = $option_id;
// $qty = isset($request->quantity) && $request->quantity != 'NaN' ? $request->quantity : 1; // $qty_check = 1; // } // // if (isset($request->type) && $request->type == 'normal') { // // dd($option); // // $attribute['names'] = $input['attr_name']; // // $attribute['option_name'] = $input['option_name']; // // $attribute['option_price'] = $input['option_price']; // // }else{ // // $option_price = array_sum($input['option_price']); // // $attribute['names'] = $input['attr_name']; // // $attribute['option_name'] = $input['option_name']; // // $attribute['option_price'] = $input['option_price']; // // }
// if (!$item) { // abort(404); // }
// if (!isset($cart[$item->id])) { // $license_name = json_decode($item->license_name, true); // $license_key = json_decode($item->license_key, true); // $cart[$item->id] = [ // 'options_id' => $option_id, // 'attribute' => $attributes, // // 'attribute_price' => $attributes[0]['price'], // 'attribute_price' => $attribute_price, // "name" => $item->name, // "slug" => $item->slug, // "qty" => $qty, // "price" => PriceHelper::grandPrice($item), // "main_price" => $item->discount_price, // "photo" => $item->photo, // "type" => $item->item_type, // "item_type" => $item->item_type, // 'item_l_n' => $item->item_type == 'license' ? end($license_name) : null, // 'item_l_k' => $item->item_type == 'license' ? end($license_key) : null, // ];
// Session::put($cartKey, $cart); // $mgs = ['message' => __('Product added successfully'), 'qty' => count(Session::get($cartKey))]; // return $mgs; // } // // dd($cart); // if (isset($cart[$item->id])) {
// if ($qty_check == 1) { // $cart[$item->id]['qty'] += $qty; // }else if(isset($request->type) && $request->type == '3'){ // $cart[$item->id]['qty'] = $qty; // } // else { // $cart[$item->id]['qty'] += $qty; // }
// if ($item->item_type == 'normal' && $item->stock <= (int) $cart[$item->id]['qty']) { // $data = ['message' => 'Product Out Of Stock', 'status' => 'outStock']; // return $data; // } // Session::put($cartKey, $cart); // $mgs = ['message' => __('Product added successfully'), 'qty' => count(Session::get($cartKey))]; // return $mgs; // }
// $mgs = ['message' => __('Product added successfully'), 'qty' => count(Session::get($cartKey))]; // return $mgs; // }
// public function store($request) // { // $user = auth()->user()->id;
// $qty_check = 0; // $input = $request->all(); // $input['option_name'] = []; // $input['option_price'] = []; // $input['attr_name'] = [];
// $qty = isset($input['quantity']) ? $input['quantity'] : 1; // $qty = is_numeric($qty) ? $qty : 1; // if ($input['options_ids']) { // foreach (explode(',', $input['options_ids']) as $optionId) { // $option = AttributeOption::findOrFail($optionId); // if ($qty > $option->stock) { // $data = ['message' => 'Product Out Of Stock', 'status' => 'outStock']; // return $data; // } // } // }
// $cartKey = 'cart_' . $user; // $cart = Session::get($cartKey); // dd($cart,$request->all()); // $item = Item::where('id', $input['item_id']) // ->select('id', 'name', 'photo', 'discount_price', 'previous_price', 'slug', 'item_type', 'license_name', 'license_key', 'stock') // ->firstOrFail();
// if ($item->item_type == 'normal' && $item->stock < $qty) { // $data = ['message' => 'Product Out Of Stock', 'status' => 'outStock']; // return $data; // }
// if ($item->item_type == 'digital' || $item->item_type == 'license') { // if (Session::has($cartKey)) { // if (array_key_exists($input['item_id'], $cart)) { // $data = ['message' => 'Product already added', 'status' => 'alreadyInCart']; // return $data; // } // } // }
// $option_id = []; // if (isset($request->type) && $request->type == '1') { // $attr_name = []; // $option_name = []; // $option_price = [];
// foreach ($item->attributes as $attr) { // if (isset($attr->options[0]->name)) { // $attr_name[] = $attr->name; // $option_name[] = $attr->options[0]->name; // $option_price[] = $attr->options[0]->price; // $option_id[] = $attr->options[0]->id; // } // }
// $input['attr_name'] = $attr_name; // $input['option_price'] = $option_price; // $input['option_name'] = $option_name; // $input['option_id'] = $option_id;
// $qty = isset($request->quantity) && $request->quantity != 'NaN' ? $request->quantity : 1; // $qty_check = 1; // } // if (!$item) { // abort(404); // }
// $option_price = array_sum($input['option_price']); // $attribute['names'] = $input['attr_name']; // $attribute['option_name'] = $input['option_name']; // $attribute['option_price'] = $input['option_price'];
// if (isset($request->item_key) && $request->item_key != (int) 0) { // $cart_item_key = explode('-', $request->item_key)[1]; // } else { // $cart_item_key = str_replace(' ', '', implode(',', $attribute['option_name'])); // }
// if (!isset($cart[$item->id . '-' . $cart_item_key])) { // $license_name = json_decode($item->license_name, true); // $license_key = json_decode($item->license_key, true); // $cart[$item->id . '-' . $cart_item_key] = [ // 'options_id' => $option_id, // 'attribute' => $attribute, // 'attribute_price' => $option_price, // "name" => $item->name, // "slug" => $item->slug, // "qty" => $qty, // "price" => PriceHelper::grandPrice($item), // "main_price" => $item->discount_price, // "photo" => $item->photo, // "type" => $item->item_type, // "item_type" => $item->item_type, // 'item_l_n' => $item->item_type == 'license' ? end($license_name) : null, // 'item_l_k' => $item->item_type == 'license' ? end($license_key) : null, // ];
// Session::put($cartKey, $cart); // $mgs = ['message' => __('Product added successfully'), 'qty' => count(Session::get($cartKey))]; // return $mgs; // }
// if (isset($cart[$item->id . '-' . $cart_item_key])) { // if ($qty_check == 1) { // $cart[$item->id . '-' . $cart_item_key]['qty'] = $qty; // } else { // $cart[$item->id . '-' . $cart_item_key]['qty'] += $qty; // }
// if ($item->item_type == 'normal' && $item->stock <= (int) $cart[$item->id . '-' . $cart_item_key]['qty']) { // $data = ['message' => 'Product Out Of Stock', 'status' => 'outStock']; // return $data; // }
// Session::put($cartKey, $cart); // $mgs = ['message' => __('Product added successfully'), 'qty' => count(Session::get($cartKey))]; // return $mgs; // }
// $mgs = ['message' => __('Product added successfully'), 'qty' => count(Session::get($cartKey))]; // return $mgs; // }
// public function store($request) // { // $user = auth()->user()->id; // $qty_check = 0; // $input = $request->all(); // $input['option_name']=[]; // $input['option_price']=[]; // $input['attr_name'] =[];
// $qty = isset($input['quantity']) ? $input['quantity'] : 1 ;
// $qty = is_numeric($qty) ? $qty : 1;
// if($input['options_ids']){ // foreach(explode(',',$input['options_ids']) as $optionId){ // $option = AttributeOption::findOrFail($optionId); // if($qty > $option->stock){ // $data = [ 'message' => 'Product Out Of Stock', 'status' => 'outStock']; // return $data; // } // } // }
// $cart = Session::get('cart'); // // dd($user,$cart);
// $item = Item::where('id',$input['item_id'])->select('id','name','photo','discount_price','previous_price','slug','item_type','license_name','license_key', 'stock')->first();
// if($item->item_type == 'normal' ){ // if($item->stock < $request->quantity){ // $data = [ 'message' => 'Product Out Of Stock', 'status' => 'outStock']; // return $data; // } // }
// $single = isset($request->type) ? ($request->type == '1' ? 1 : 0 ) : 0;
// if(Session::has('cart')){ // if($item->item_type == 'digital' || $item->item_type == 'license'){ // $check = array_key_exists($input['item_id'],Session::get('cart'));
// if($check){ // $data = [ 'message' => 'Product already added', 'status' => 'alreadyInCart']; // return $data; // }else{ // if(array_key_exists($input['item_id'].'-',Session::get('cart'))){
// $data = [ 'message' => 'Product already added', 'status' => 'alreadyInCart']; // return $data; // } // } // }
// }
// $option_id = [];
// if($single == 1){ // $attr_name = []; // $option_name = []; // $option_price = [];
// if(count($item->attributes) > 0){ // foreach($item->attributes as $attr){ // if(isset($attr->options[0]->name)){ // $attr_name[] = $attr->name; // $option_name[] = $attr->options[0]->name; // $option_price[] = $attr->options[0]->price; // $option_id[] = $attr->options[0]->id; // } // } // }
// $input['attr_name'] = $attr_name; // $input['option_price'] = $option_price; // $input['option_name'] = $option_name; // $input['option_id'] = $option_id;
// if($request->quantity != 'NaN'){ // $qty = $request->quantity; // $qty_check = 1; // }else{ // $qty = 1; // }
// }else{
// if($input['attribute_ids']){ // foreach(explode(',',$input['attribute_ids']) as $attrId){ // $attr = Attribute::findOrFail($attrId); // $attr_name[] = $attr->name; // } // $input['attr_name'] = $attr_name; // }
// if($input['options_ids']){ // foreach(explode(',',$input['options_ids']) as $optionId){ // $option = AttributeOption::findOrFail($optionId); // $option_name[] = $option->name; // $option_price[] = $option->price; // $option_id[] = $option->id; // } // $input['option_name'] = $option_name; // $input['option_price'] = $option_price; // }
// }
// if (!$item) { // abort(404); // }
// $option_price = array_sum($input['option_price']); // $attribute['names'] = $input['attr_name']; // $attribute['option_name'] = $input['option_name'];
// if(isset($request->item_key) && $request->item_key !=(int) 0){ // $cart_item_key = explode('-',$request->item_key)[1];
// }else{ // $cart_item_key = str_replace(' ','',implode(',',$attribute['option_name'])); // }
// $attribute['option_price'] = $input['option_price']; // $cart = Session::get('cart'); // // if cart is empty then this the first product // if (!$cart || !isset($cart[$item->id.'-'.$cart_item_key])) { // $license_name = json_decode($item->license_name,true); // $license_key = json_decode($item->license_name,true); // $cart [$item->id.'-'.$cart_item_key] = [ // 'options_id' => $option_id, // 'attribute' => $attribute, // 'attribute_price' => $option_price, // "name" => $item->name, // "slug" => $item->slug, // "qty" => $qty, // "price" => PriceHelper::grandPrice($item), // "main_price" => $item->discount_price, // "photo" => $item->photo, // "type" => $item->item_type, // "item_type" => $item->item_type, // 'item_l_n' => $item->item_type == 'license' ? end($license_name) : null, // 'item_l_k' => $item->item_type == 'license' ? end($license_key) : null // ];
// Session::put('cart', $cart); // $mgs = ['message' => __('Product add successfully') , 'qty' => count(Session::get('cart'))]; // return $mgs;
// }
// // if cart not empty then check if this product exist then increment quantity // if (isset($cart[$item->id.'-'.$cart_item_key])) {
// $cart = Session::get('cart');
// if($qty_check == 1){ // $cart[$item->id.'-'.$cart_item_key]['qty'] = $qty; // }else{ // $cart[$item->id.'-'.$cart_item_key]['qty'] += $qty; // }
// if($item->item_type == 'normal' ){
// if($item->stock <= (int)$cart[$item->id.'-'.$cart_item_key]['qty']){ // $data = [ 'message' => 'Product Out Of Stock', 'status' => 'outStock']; // return $data; // } // }
// Session::put('cart', $cart);
// if($qty_check == 1){ // $mgs = ['message' => __('Product add successfully') , 'qty' => count(Session::get('cart'))]; // }else{ // $mgs = ['message' => __('Product add successfully') , 'qty' => count(Session::get('cart'))]; // }
// $qty_check = 0; // return $mgs; // }
// $mgs = ['message' => __('Product add successfully') , 'qty' => count(Session::get('cart'))]; // return $mgs;
// }
public function promoStore($request) { $user = auth()->user()->id;
$input = $request->all(); $promo_code = PromoCode::where('status', 1)->whereCodeName($input['code'])->where('no_of_times', '>', 0)->first();
if ($promo_code) { $cart = Session::get('cart_' . $user); $cartTotal = PriceHelper::cartTotal($cart, 2); $discount = $this->getDiscount($promo_code->discount, $promo_code->type, $cartTotal);
$coupon = [ 'discount' => $discount['sub'], 'code' => $promo_code ]; Session::put('coupon', $coupon);
return [ 'status' => true, 'message' => __('Promo code found!') ]; } else { return [ 'status' => false, 'message' => __('No coupon code found') ]; } }
public function getCart() { $cart = Session::has('cart') ? Session::get('cart') : null; return $cart;
}
public function getDiscount($discount, $type, $price) { if ($type == 'amount') { $sub = $discount; $total = $price - $sub; } else { $val = $price / 100; $sub = $val * $discount; $total = $price - $sub; }
return [ 'sub' => $sub, 'total' => $total ]; }
}
|