示例#1
0
 public function checkout(Request $request)
 {
     if ($this->user_session->is_logged_in) {
         $user_id = $this->user_session->user_id;
     } else {
         $user_id = null;
     }
     $objUser = $this->user_session;
     //get cart details
     if ($this->user_session->is_logged_in) {
         $arrCartItems = DB::connection('mongodb')->collection('cart')->where('user_id', $user_id)->orWhere('session_id', session_id())->get();
     } else {
         $arrCartItems = DB::connection('mongodb')->collection('cart')->where('session_id', session_id())->get();
     }
     //get states and countries from database
     $arrStates = DB::connection('mongodb')->collection('states')->get();
     $arrCountries = DB::connection('mongodb')->collection('countries')->get();
     $arrStates_code_state = getKeyValueArray('code', 'state', $arrStates);
     $arrCountries_code_country = getKeyValueArray('code', 'country', $arrCountries);
     $form['fname'] = filter_form_input($request->input('fname', ''));
     $form['lname'] = filter_form_input($request->input('lname', ''));
     $form['email'] = filter_form_input($request->input('email', ''));
     $form['mobile'] = filter_form_input($request->input('mobile', ''));
     $form['zip'] = filter_form_input($request->input('zip', ''));
     $form['address'] = filter_form_input($request->input('address1', '') . ' ' . $request->input('address2', ''));
     $form['city'] = filter_form_input($request->input('city', ''));
     $form['state'] = filter_form_input($request->input('state', ''));
     $form['country'] = filter_form_input($request->input('country', ''));
     $message = '';
     $open_tab = '';
     if ($request->isMethod('post')) {
         if ($form['fname'] != '' && $form['lname'] != '' && $form['email'] != '' && $form['email'] != '' && $form['mobile'] != '' && $form['zip'] != '' && $form['address'] != '' && $form['city'] != '') {
             if (!filter_var($form['email'], FILTER_VALIDATE_EMAIL)) {
                 $message[] = '-Please enter a valid email address.';
             }
             if (strlen($form['mobile']) > 10 || strlen($form['mobile']) < 10) {
                 $message[] = '-Please enter valid 10 digit mobile number.';
             }
             if (!is_numeric($form['mobile'])) {
                 $message[] = '-Please enter valid 10 digit mobile number.';
             }
             if ($form['state'] == 'select' || $form['country'] == 'select' || !array_key_exists($form['state'], $arrStates_code_state) || !array_key_exists($form['country'], $arrCountries_code_country)) {
                 $message[] = '-Please select your state and country.';
             }
             if (empty($message)) {
                 try {
                     //save user in database
                     $objUserMaster = new App\UserMaster();
                     $objUserMaster->fname = $form['fname'];
                     $objUserMaster->lname = $form['lname'];
                     $objUserMaster->email = $form['email'];
                     $objUserMaster->mobile = $form['mobile'];
                     $objUserMaster->zip_code = $form['zip'];
                     $objUserMaster->state = $form['state'];
                     $objUserMaster->country = $form['country'];
                     $objUserMaster->city = $form['city'];
                     $objUserMaster->address = $form['address'];
                     $objUserMaster->password = md5(time());
                     $res = $objUserMaster->save();
                     $LastInsertId = $objUserMaster->user_id;
                 } catch (\Exception $e) {
                     $message[] = 'Looks like something went wrong or mostly the email you entered, already exists. Kindly register with different email id or contact our support at <a class="yellow-link" href="mailto:' . config('global.support_email') . '">' . config('global.support_email') . '</a>';
                     $open_tab = 'shippingAddressLink';
                 }
                 if (isset($res) && $res == true && empty($message)) {
                     $objUser = $objUserMaster;
                     $user_id = $objUser->user_id;
                 } else {
                     if (empty($message)) {
                         $message[] = 'Something went wrong. Please try again.';
                         $open_tab = 'shippingAddressLink';
                     }
                 }
             }
         } else {
             $message[] = '-Please fill in all the fields.';
             $open_tab = 'shippingAddressLink';
         }
     }
     if ($user_id != null) {
         //set userid as current user in all the cart items for current session
         foreach ($arrCartItems as $key => $item) {
             $objCartItem = App\CartMaster::where('item_id', $item['item_id'])->first();
             $objCartItem->user_id = $user_id;
             $objCartItem->save();
         }
     }
     if (is_array($message)) {
         $temp = '';
         foreach ($message as $value) {
             $temp = $temp . $value . '<br>';
         }
         $message = $temp;
     }
     $cart_total_price = 0;
     $cart_total_items = 0;
     foreach ($arrCartItems as $key => $value) {
         $cart_total_price = $cart_total_price + $value['price'] * $value['qty'];
         $cart_total_items++;
     }
     //shipping charges and vat
     $shipping_charges = 0;
     $vat = 0;
     $total_price = $cart_total_price + $shipping_charges + $vat;
     return view('user.checkout', compact('total_price', 'vat', 'shipping_charges', 'cart_total_items', 'cart_total_price', 'open_tab', 'form', 'arrCountries', 'arrStates', 'message', 'user_id', 'objUser', 'arrStates_code_state', 'arrCountries_code_country'));
 }
示例#2
0
 public function index(Request $request, $item_id = '')
 {
     if ($item_id != '') {
         $objItem = App\ItemMaster::where('item_id', $item_id)->where('status', 1)->first();
         if (null != $objItem) {
             //fetch all the categories
             $objCategory = App\CategoryMaster::all();
             //fetch all the item types
             $objType = App\TypeMaster::all();
             //get category id versus category name array
             $arrCetegory_id_obj = getKeyValueArray('cat_id', 'name', $objCategory, 'object', true);
             $arrType_id_obj = getKeyValueArray('type_id', 'name', $objType, 'object', true);
             //get coupon information if any
             $objCouponMaster = DB::table('coupon_master')->join('coupon_type_master', 'coupon_type_master.coupon_id', '=', 'coupon_master.coupon_id')->where('coupon_master.status', 1)->where('coupon_type_master.type_id', $objItem->item_type)->select('coupon_master.*')->get();
             $size = null;
             $message = '';
             $show_size_chart = false;
             if ($objItem->qty == null) {
                 $show_size_chart = true;
             }
             if ($show_size_chart == true) {
                 //get item sizes
                 $objSizes = DB::table('item_size_master')->where('item_id', $objItem->item_id)->join('size_master', 'size_master.size_id', '=', 'item_size_master.size_id')->select('item_size_master.*', 'size_master.*')->get();
                 $arrSize_id_size_code = getKeyValueArray('size_id', 'size_code', $objSizes, 'object', false);
             }
             //get total available quantity
             $total_available_qty = 0;
             if ($show_size_chart) {
                 foreach ($objSizes as $key => $value) {
                     $total_available_qty = $total_available_qty + $value->qty;
                 }
             } else {
                 if ($objItem->qty != null) {
                     $total_available_qty = $objItem->qty;
                 }
             }
             //add to cart
             if ($request->isMethod('post')) {
                 //check size is valid
                 if ($show_size_chart == true) {
                     $size = filter_form_input($request->input('size', ''));
                     $size_valid = false;
                     foreach ($objSizes as $key => $value) {
                         if ($value->qty > 0 && $value->size_code == $size) {
                             $size_valid = true;
                         }
                     }
                     if (!$size_valid) {
                         $message[] = 'Plese select size first.';
                     }
                 }
                 $qty = filter_form_input($request->input('qty', ''));
                 if ($message == '') {
                     if ($qty != '' && is_numeric($qty) && $qty <= $total_available_qty) {
                         if ($this->user_session->is_logged_in == true) {
                             $user_id = $this->user_session->user_id;
                         } else {
                             $user_id = null;
                         }
                         if ($this->user_session->is_logged_in) {
                             $check_if_already_added = App\CartMaster::where(function ($query) {
                                 $query->where('user_id', (int) $this->user_session->user_id)->orWhere('session_id', session_id());
                             })->where('item_id', (int) $objItem->item_id)->first();
                         } else {
                             $check_if_already_added = App\CartMaster::where('session_id', session_id())->where('item_id', (int) $objItem->item_id)->first();
                         }
                         if (null == $check_if_already_added) {
                             //add to cart
                             DB::connection('mongodb')->table('cart')->insert(['item_id' => (int) $objItem->item_id, 'user_id' => (int) $user_id, 'session_id' => session_id(), 'qty' => (int) $qty, 'size' => $size, 'price' => $objItem->price, 'coupon_added' => 0]);
                             return redirect()->route('cart');
                         } else {
                             $message[] = 'You already have added this item to your cart.';
                         }
                     } else {
                         $message[] = 'Oops! the selected quantity is not available. Plese select lesser quantity.';
                     }
                 }
             }
             //other product images
             $objProductImages = array();
             if (is_array($message)) {
                 $temp = '';
                 foreach ($message as $value) {
                     $temp = $temp . $value . '<br>';
                 }
                 $message = $temp;
             }
             return view('product.product', compact('objCouponMaster', 'total_available_qty', 'message', 'show_size_chart', 'objSizes', 'objProductImages', 'objItem', 'objCategory', 'objType', 'arrCetegory_id_obj', 'arrType_id_obj'));
         } else {
             abort(404);
         }
     } else {
         abort(404);
     }
 }