function ct_tour_order_save_action()
 {
     //validation
     if (!isset($_POST['order_save']) || !wp_verify_nonce($_POST['order_save'], 'ct_manage_orders')) {
         print esc_html__('Sorry, your nonce did not verify.', 'citytours');
         exit;
     }
     if (empty($_POST['post_id']) || 'tour' != get_post_type($_POST['post_id'])) {
         print esc_html__('Invalide Tour ID.', 'citytours');
         exit;
     }
     global $wpdb;
     $default_order_data = ct_order_default_order_data('update');
     $order_data = array();
     foreach ($default_order_data as $table_field => $def_value) {
         if (isset($_POST[$table_field])) {
             $order_data[$table_field] = $_POST[$table_field];
             if (!is_array($_POST[$table_field])) {
                 $order_data[$table_field] = sanitize_text_field($order_data[$table_field]);
             } else {
                 $order_data[$table_field] = serialize($order_data[$table_field]);
             }
         }
     }
     $order_data = array_replace($default_order_data, $order_data);
     $order_data['post_id'] = ct_tour_org_id($order_data['post_id']);
     if (empty($_POST['id'])) {
         //insert
         $order_data['created'] = date('Y-m-d H:i:s');
         $order_data['post_type'] = 'tour';
         $wpdb->insert(CT_ORDER_TABLE, $order_data);
         $order_id = $wpdb->insert_id;
     } else {
         //update
         $wpdb->update(CT_ORDER_TABLE, $order_data, array('id' => sanitize_text_field($_POST['id'])));
         $order_id = sanitize_text_field($_POST['id']);
     }
     $tour_data = array('tour_id' => $order_data['post_id'], 'tour_date' => $order_data['date_from'], 'adults' => $order_data['total_adults'], 'kids' => $order_data['total_kids'], 'total_price' => $order_data['total_price'], 'order_id' => $order_id);
     // update tour booking table
     $sql = 'DELETE FROM ' . CT_TOUR_BOOKINGS_TABLE . ' WHERE order_id=%d';
     $wpdb->query($wpdb->prepare($sql, $order_id));
     $format = array('%d', '%s', '%d', '%d', '%f', '%d');
     if (!empty($_POST['tour_booking_id'])) {
         $tour_data['id'] = $_POST['tour_booking_id'];
         $format[] = '%d';
     }
     $wpdb->insert(CT_TOUR_BOOKINGS_TABLE, $tour_data, $format);
     // add additional services
     // update service table
     if (!empty($_POST['service_id'])) {
         $service_id_list = $_POST['service_id'];
         $service_qty_list = $_POST['service_qty'];
         $service_price_list = $_POST['service_price'];
         $service_booking_id_list = $_POST['service_booking_id'];
         $sql = 'DELETE FROM ' . CT_ADD_SERVICES_BOOKINGS_TABLE . ' WHERE order_id=%d';
         $wpdb->query($wpdb->prepare($sql, $order_id));
         for ($index = 0; $index < count($service_id_list); $index++) {
             $service_data = array('add_service_id' => $service_id_list[$index], 'qty' => $service_qty_list[$index], 'total_price' => $service_price_list[$index], 'order_id' => $order_id);
             $format = array('%d', '%d', '%f', '%d');
             if (!empty($service_booking_id_list[$index])) {
                 $service_data['id'] = $service_booking_id_list[$index];
                 $format[] = '%d';
             }
             $wpdb->insert(CT_ADD_SERVICES_BOOKINGS_TABLE, $service_data, $format);
             // add additional services
         }
     }
     wp_redirect(admin_url('edit.php?post_type=tour&page=tour_orders&action=edit&order_id=' . $order_id . '&updated=true'));
     exit;
 }
Exemple #2
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);
 }