Example #1
0
 function ct_tour_submit_booking()
 {
     global $wpdb, $ct_options;
     // validation
     $result_json = array('success' => 0, 'result' => '');
     if (!isset($_POST['uid']) || !CT_Hotel_Cart::get($_POST['uid'])) {
         $result_json['success'] = 0;
         $result_json['result'] = esc_html__('Sorry, some error occurred on input data validation.', 'citytours');
         wp_send_json($result_json);
     }
     if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'checkout')) {
         $result_json['success'] = 0;
         $result_json['result'] = esc_html__('Sorry, your nonce did not verify.', 'citytours');
         wp_send_json($result_json);
     }
     // init variables
     $uid = $_POST['uid'];
     $post_fields = array('first_name', 'last_name', 'email', 'phone', 'country', 'address1', 'address2', 'city', 'state', 'zip');
     $order_info = ct_order_default_order_data('new');
     foreach ($post_fields as $post_field) {
         if (!empty($_POST[$post_field])) {
             $order_info[$post_field] = sanitize_text_field($_POST[$post_field]);
         }
     }
     $latest_order_id = $wpdb->get_var('SELECT id FROM ' . CT_ORDER_TABLE . ' ORDER BY id DESC LIMIT 1');
     $booking_no = mt_rand(1000, 9999);
     $booking_no .= $latest_order_id;
     $pin_code = mt_rand(1000, 9999);
     $cart_data = CT_Hotel_Cart::get($uid);
     $order_info['total_price'] = $cart_data['total_price'];
     $order_info['total_adults'] = $cart_data['total_adults'];
     $order_info['total_kids'] = $cart_data['total_kids'];
     $order_info['status'] = 'new';
     // new
     $order_info['deposit_paid'] = 1;
     $order_info['mail_sent'] = 0;
     $order_info['post_id'] = $cart_data['tour_id'];
     if (!empty($cart_data['date'])) {
         $order_info['date_from'] = date('Y-m-d', ct_strtotime($cart_data['date']));
     }
     $order_info['booking_no'] = $booking_no;
     $order_info['pin_code'] = $pin_code;
     // calculate deposit payment
     $deposit_rate = get_post_meta($cart_data['tour_id'], '_tour_security_deposit', true);
     // if woocommerce enabled change currency_code and exchange rate as default
     if (!empty($deposit_rate) && ct_is_woo_enabled()) {
         $order_info['currency_code'] = ct_get_def_currency();
         $order_info['exchange_rate'] = 1;
     } else {
         if (!isset($_SESSION['exchange_rate'])) {
             ct_init_currency();
         }
         $order_info['exchange_rate'] = $_SESSION['exchange_rate'];
         $order_info['currency_code'] = ct_get_user_currency();
     }
     // if payment enabled set deposit price field
     if (!empty($deposit_rate) && ct_is_payment_enabled()) {
         $order_info['deposit_price'] = $deposit_rate / 100 * $order_info['total_price'] * $order_info['exchange_rate'];
         $order_info['deposit_paid'] = 0;
         // set unpaid if payment enabled
         $order_info['status'] = 'pending';
     }
     $order_info['created'] = date('Y-m-d H:i:s');
     $order_info['post_type'] = 'tour';
     if ($wpdb->insert(CT_ORDER_TABLE, $order_info)) {
         CT_Hotel_Cart::_unset($uid);
         $order_id = $wpdb->insert_id;
         if (!empty($cart_data['tour'])) {
             $tour_booking_info = array();
             $tour_booking_info['order_id'] = $order_id;
             $tour_booking_info['tour_id'] = $cart_data['tour_id'];
             $tour_booking_info['tour_date'] = $cart_data['date'];
             $tour_booking_info['adults'] = $cart_data['tour']['adults'];
             $tour_booking_info['kids'] = $cart_data['tour']['kids'];
             $tour_booking_info['total_price'] = $cart_data['tour']['total'];
             $wpdb->insert(CT_TOUR_BOOKINGS_TABLE, $tour_booking_info);
         }
         if (!empty($cart_data['add_service'])) {
             foreach ($cart_data['add_service'] as $service_id => $service_data) {
                 $service_booking_info = array();
                 $service_booking_info['order_id'] = $order_id;
                 $service_booking_info['add_service_id'] = $service_id;
                 $service_booking_info['qty'] = $service_data['qty'];
                 $service_booking_info['total_price'] = $service_data['total'];
                 $wpdb->insert(CT_ADD_SERVICES_BOOKINGS_TABLE, $service_booking_info);
             }
         }
         $result_json['success'] = 1;
         $result_json['result']['order_id'] = $order_id;
         $result_json['result']['booking_no'] = $booking_no;
         $result_json['result']['pin_code'] = $pin_code;
     } else {
         $result_json['success'] = 0;
         $result_json['result'] = esc_html__('Sorry, An error occurred while add your order.', 'citytours');
     }
     wp_send_json($result_json);
 }
Example #2
0
 function ct_price($amount, $type = "", $currency = '', $convert = 1)
 {
     global $ct_options;
     $exchange_rate = 1;
     $currency_symbol = '';
     if (empty($currency)) {
         if (!isset($_SESSION['exchange_rate']) || !isset($_SESSION['currency_symbol'])) {
             ct_init_currency();
         }
         $exchange_rate = $_SESSION['exchange_rate'];
         $currency_symbol = $_SESSION['currency_symbol'];
     } else {
         $exchange_rate = ct_currency_converter(1, ct_get_def_currency(), $currency);
         $currency_symbol = ct_get_currency_symbol($currency);
     }
     if ($convert) {
         $amount *= $exchange_rate;
     }
     $decimal_prec = isset($ct_options['decimal_prec']) ? $ct_options['decimal_prec'] : 2;
     $decimal_sep = isset($ct_options['decimal_sep']) ? $ct_options['decimal_sep'] : '.';
     $thousands_sep = isset($ct_options['thousands_sep']) ? $ct_options['thousands_sep'] : ',';
     $price_label = number_format($amount, $decimal_prec, $decimal_sep, $thousands_sep);
     $format = ct_get_price_format($type);
     return sprintf($format, $currency_symbol, $price_label);
 }