CartController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Product;
  5. use App\Models\Category;
  6. use App\Models\Cart;
  7. use Auth;
  8. use Session;
  9. use Cookie;
  10. class CartController extends Controller
  11. {
  12. public function index(Request $request)
  13. {
  14. if(auth()->user() != null) {
  15. $user_id = Auth::user()->id;
  16. if($request->session()->get('temp_user_id')) {
  17. Cart::where('temp_user_id', $request->session()->get('temp_user_id'))
  18. ->update(
  19. [
  20. 'user_id' => $user_id,
  21. 'temp_user_id' => null
  22. ]
  23. );
  24. Session::forget('temp_user_id');
  25. }
  26. $carts = Cart::where('user_id', $user_id)->get();
  27. } else {
  28. $temp_user_id = $request->session()->get('temp_user_id');
  29. // $carts = Cart::where('temp_user_id', $temp_user_id)->get();
  30. $carts = ($temp_user_id != null) ? Cart::where('temp_user_id', $temp_user_id)->get() : [] ;
  31. }
  32. return view('frontend.view_cart', compact('carts'));
  33. }
  34. public function showCartModal(Request $request)
  35. {
  36. $product = Product::find($request->id);
  37. return view('frontend.partials.addToCart', compact('product'));
  38. }
  39. public function showCartModalAuction(Request $request)
  40. {
  41. $product = Product::find($request->id);
  42. return view('auction.frontend.addToCartAuction', compact('product'));
  43. }
  44. public function addToCart(Request $request)
  45. {
  46. $product = Product::find($request->id);
  47. $carts = array();
  48. $data = array();
  49. if(auth()->user() != null) {
  50. $user_id = Auth::user()->id;
  51. $data['user_id'] = $user_id;
  52. $carts = Cart::where('user_id', $user_id)->get();
  53. } else {
  54. if($request->session()->get('temp_user_id')) {
  55. $temp_user_id = $request->session()->get('temp_user_id');
  56. } else {
  57. $temp_user_id = bin2hex(random_bytes(10));
  58. $request->session()->put('temp_user_id', $temp_user_id);
  59. }
  60. $data['temp_user_id'] = $temp_user_id;
  61. $carts = Cart::where('temp_user_id', $temp_user_id)->get();
  62. }
  63. $data['product_id'] = $product->id;
  64. $data['owner_id'] = $product->user_id;
  65. $str = '';
  66. $tax = 0;
  67. if($product->auction_product == 0){
  68. if($product->digital != 1 && $request->quantity < $product->min_qty) {
  69. return array(
  70. 'status' => 0,
  71. 'cart_count' => count($carts),
  72. 'modal_view' => view('frontend.partials.minQtyNotSatisfied', [ 'min_qty' => $product->min_qty ])->render(),
  73. 'nav_cart_view' => view('frontend.partials.cart')->render(),
  74. );
  75. }
  76. //check the color enabled or disabled for the product
  77. if($request->has('color')) {
  78. $str = $request['color'];
  79. }
  80. if ($product->digital != 1) {
  81. //Gets all the choice values of customer choice option and generate a string like Black-S-Cotton
  82. foreach (json_decode(Product::find($request->id)->choice_options) as $key => $choice) {
  83. if($str != null){
  84. $str .= '-'.str_replace(' ', '', $request['attribute_id_'.$choice->attribute_id]);
  85. }
  86. else{
  87. $str .= str_replace(' ', '', $request['attribute_id_'.$choice->attribute_id]);
  88. }
  89. }
  90. }
  91. $data['variation'] = $str;
  92. $product_stock = $product->stocks->where('variant', $str)->first();
  93. $price = $product_stock->price;
  94. if($product->wholesale_product){
  95. $wholesalePrice = $product_stock->wholesalePrices->where('min_qty', '<=', $request->quantity)->where('max_qty', '>=', $request->quantity)->first();
  96. if($wholesalePrice){
  97. $price = $wholesalePrice->price;
  98. }
  99. }
  100. $quantity = $product_stock->qty;
  101. if($quantity < $request['quantity']){
  102. return array(
  103. 'status' => 0,
  104. 'cart_count' => count($carts),
  105. 'modal_view' => view('frontend.partials.outOfStockCart')->render(),
  106. 'nav_cart_view' => view('frontend.partials.cart')->render(),
  107. );
  108. }
  109. //discount calculation
  110. $discount_applicable = false;
  111. if ($product->discount_start_date == null) {
  112. $discount_applicable = true;
  113. }
  114. elseif (strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
  115. strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date) {
  116. $discount_applicable = true;
  117. }
  118. if ($discount_applicable) {
  119. if($product->discount_type == 'percent'){
  120. $price -= ($price*$product->discount)/100;
  121. }
  122. elseif($product->discount_type == 'amount'){
  123. $price -= $product->discount;
  124. }
  125. }
  126. //calculation of taxes
  127. foreach ($product->taxes as $product_tax) {
  128. if($product_tax->tax_type == 'percent'){
  129. $tax += ($price * $product_tax->tax) / 100;
  130. }
  131. elseif($product_tax->tax_type == 'amount'){
  132. $tax += $product_tax->tax;
  133. }
  134. }
  135. $data['quantity'] = $request['quantity'];
  136. $data['price'] = $price;
  137. $data['tax'] = $tax;
  138. //$data['shipping'] = 0;
  139. $data['shipping_cost'] = 0;
  140. $data['product_referral_code'] = null;
  141. $data['cash_on_delivery'] = $product->cash_on_delivery;
  142. $data['digital'] = $product->digital;
  143. if ($request['quantity'] == null){
  144. $data['quantity'] = 1;
  145. }
  146. if(Cookie::has('referred_product_id') && Cookie::get('referred_product_id') == $product->id) {
  147. $data['product_referral_code'] = Cookie::get('product_referral_code');
  148. }
  149. if($carts && count($carts) > 0){
  150. $foundInCart = false;
  151. foreach ($carts as $key => $cartItem){
  152. $cart_product = Product::where('id', $cartItem['product_id'])->first();
  153. if($cart_product->auction_product == 1){
  154. return array(
  155. 'status' => 0,
  156. 'cart_count' => count($carts),
  157. 'modal_view' => view('frontend.partials.auctionProductAlredayAddedCart')->render(),
  158. 'nav_cart_view' => view('frontend.partials.cart')->render(),
  159. );
  160. }
  161. if($cartItem['product_id'] == $request->id) {
  162. $product_stock = $cart_product->stocks->where('variant', $str)->first();
  163. $quantity = $product_stock->qty;
  164. if($quantity < $cartItem['quantity'] + $request['quantity']){
  165. return array(
  166. 'status' => 0,
  167. 'cart_count' => count($carts),
  168. 'modal_view' => view('frontend.partials.outOfStockCart')->render(),
  169. 'nav_cart_view' => view('frontend.partials.cart')->render(),
  170. );
  171. }
  172. if(($str != null && $cartItem['variation'] == $str) || $str == null){
  173. $foundInCart = true;
  174. $cartItem['quantity'] += $request['quantity'];
  175. if($cart_product->wholesale_product){
  176. $wholesalePrice = $product_stock->wholesalePrices->where('min_qty', '<=', $request->quantity)->where('max_qty', '>=', $request->quantity)->first();
  177. if($wholesalePrice){
  178. $price = $wholesalePrice->price;
  179. }
  180. }
  181. $cartItem['price'] = $price;
  182. $cartItem->save();
  183. }
  184. }
  185. }
  186. if (!$foundInCart) {
  187. Cart::create($data);
  188. }
  189. }
  190. else{
  191. Cart::create($data);
  192. }
  193. if(auth()->user() != null) {
  194. $user_id = Auth::user()->id;
  195. $carts = Cart::where('user_id', $user_id)->get();
  196. } else {
  197. $temp_user_id = $request->session()->get('temp_user_id');
  198. $carts = Cart::where('temp_user_id', $temp_user_id)->get();
  199. }
  200. return array(
  201. 'status' => 1,
  202. 'cart_count' => count($carts),
  203. 'modal_view' => view('frontend.partials.addedToCart', compact('product', 'data'))->render(),
  204. 'nav_cart_view' => view('frontend.partials.cart')->render(),
  205. );
  206. }
  207. else{
  208. $price = $product->bids->max('amount');
  209. foreach ($product->taxes as $product_tax) {
  210. if($product_tax->tax_type == 'percent'){
  211. $tax += ($price * $product_tax->tax) / 100;
  212. }
  213. elseif($product_tax->tax_type == 'amount'){
  214. $tax += $product_tax->tax;
  215. }
  216. }
  217. $data['quantity'] = 1;
  218. $data['price'] = $price;
  219. $data['tax'] = $tax;
  220. $data['shipping_cost'] = 0;
  221. $data['product_referral_code'] = null;
  222. $data['cash_on_delivery'] = $product->cash_on_delivery;
  223. $data['digital'] = $product->digital;
  224. if(count($carts) == 0){
  225. Cart::create($data);
  226. }
  227. if(auth()->user() != null) {
  228. $user_id = Auth::user()->id;
  229. $carts = Cart::where('user_id', $user_id)->get();
  230. } else {
  231. $temp_user_id = $request->session()->get('temp_user_id');
  232. $carts = Cart::where('temp_user_id', $temp_user_id)->get();
  233. }
  234. return array(
  235. 'status' => 1,
  236. 'cart_count' => count($carts),
  237. 'modal_view' => view('frontend.partials.addedToCart', compact('product', 'data'))->render(),
  238. 'nav_cart_view' => view('frontend.partials.cart')->render(),
  239. );
  240. }
  241. }
  242. //removes from Cart
  243. public function removeFromCart(Request $request)
  244. {
  245. Cart::destroy($request->id);
  246. if(auth()->user() != null) {
  247. $user_id = Auth::user()->id;
  248. $carts = Cart::where('user_id', $user_id)->get();
  249. } else {
  250. $temp_user_id = $request->session()->get('temp_user_id');
  251. $carts = Cart::where('temp_user_id', $temp_user_id)->get();
  252. }
  253. return array(
  254. 'cart_count' => count($carts),
  255. 'cart_view' => view('frontend.partials.cart_details', compact('carts'))->render(),
  256. 'nav_cart_view' => view('frontend.partials.cart')->render(),
  257. );
  258. }
  259. //updated the quantity for a cart item
  260. public function updateQuantity(Request $request)
  261. {
  262. $cartItem = Cart::findOrFail($request->id);
  263. if($cartItem['id'] == $request->id){
  264. $product = Product::find($cartItem['product_id']);
  265. $product_stock = $product->stocks->where('variant', $cartItem['variation'])->first();
  266. $quantity = $product_stock->qty;
  267. $price = $product_stock->price;
  268. //discount calculation
  269. $discount_applicable = false;
  270. if ($product->discount_start_date == null) {
  271. $discount_applicable = true;
  272. }
  273. elseif (strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
  274. strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date) {
  275. $discount_applicable = true;
  276. }
  277. if ($discount_applicable) {
  278. if($product->discount_type == 'percent'){
  279. $price -= ($price*$product->discount)/100;
  280. }
  281. elseif($product->discount_type == 'amount'){
  282. $price -= $product->discount;
  283. }
  284. }
  285. if($quantity >= $request->quantity) {
  286. if($request->quantity >= $product->min_qty){
  287. $cartItem['quantity'] = $request->quantity;
  288. }
  289. }
  290. if($product->wholesale_product){
  291. $wholesalePrice = $product_stock->wholesalePrices->where('min_qty', '<=', $request->quantity)->where('max_qty', '>=', $request->quantity)->first();
  292. if($wholesalePrice){
  293. $price = $wholesalePrice->price;
  294. }
  295. }
  296. $cartItem['price'] = $price;
  297. $cartItem->save();
  298. }
  299. if(auth()->user() != null) {
  300. $user_id = Auth::user()->id;
  301. $carts = Cart::where('user_id', $user_id)->get();
  302. } else {
  303. $temp_user_id = $request->session()->get('temp_user_id');
  304. $carts = Cart::where('temp_user_id', $temp_user_id)->get();
  305. }
  306. return array(
  307. 'cart_count' => count($carts),
  308. 'cart_view' => view('frontend.partials.cart_details', compact('carts'))->render(),
  309. 'nav_cart_view' => view('frontend.partials.cart')->render(),
  310. );
  311. }
  312. }