function order_item_meta($values, $order)
 {
     global $wpdb;
     $currency_symbol = get_woocommerce_currency_symbol();
     $product_id = $values['product_id'];
     $quantity = $values['quantity'];
     $booking = $values['booking'];
     $order_item_id = $order->order_item_id;
     $order_id = $order->order_id;
     $global_settings = json_decode(get_option('woocommerce_booking_global_settings'));
     $booking_settings = get_post_meta($product_id, 'woocommerce_booking_settings', true);
     if (isset($booking_settings['booking_fixed_block_enable']) && isset($booking_settings['booking_partial_payment_radio']) && is_plugin_active("bkap-deposits/deposits.php")) {
         if (isset($global_settings->enable_rounding) && $global_settings->enable_rounding == "on") {
             woocommerce_add_order_item_meta($order_item_id, book_t('book.item-partial-total'), $currency_symbol . round($values['booking'][0]['Total'] * $values['quantity']), true);
             woocommerce_add_order_item_meta($order_item_id, book_t('book.item-partial-deposit'), $currency_symbol . round($values['booking'][0]['Deposit'] * $values['quantity']), true);
             woocommerce_add_order_item_meta($order_item_id, book_t('book.item-partial-remaining'), $currency_symbol . round($values['booking'][0]['Remaining'] * $values['quantity']), true);
         } else {
             woocommerce_add_order_item_meta($order_item_id, book_t('book.item-partial-total'), $currency_symbol . $values['booking'][0]['Total'] * $values['quantity'], true);
             woocommerce_add_order_item_meta($order_item_id, book_t('book.item-partial-deposit'), $currency_symbol . $values['booking'][0]['Deposit'] * $values['quantity'], true);
             woocommerce_add_order_item_meta($order_item_id, book_t('book.item-partial-remaining'), $currency_symbol . $values['booking'][0]['Remaining'] * $values['quantity'], true);
         }
     }
 }
 function bkap_quantity_check()
 {
     global $woocommerce, $wpdb;
     foreach ($woocommerce->cart->cart_contents as $key => $value) {
         $duplicate_of = get_post_meta($value['product_id'], '_icl_lang_duplicate_of', true);
         if ($duplicate_of == '' && $duplicate_of == null) {
             $post_time = get_post($value['product_id']);
             $id_query = "SELECT ID FROM `" . $wpdb->prefix . "posts` WHERE post_date = '" . $post_time->post_date . "' ORDER BY ID LIMIT 1";
             $results_post_id = $wpdb->get_results($id_query);
             if (isset($results_post_id)) {
                 $duplicate_of = $results_post_id[0]->ID;
             } else {
                 $duplicate_of = $value['product_id'];
             }
             //$duplicate_of = $item_value['product_id'];
         }
         $booking_settings = get_post_meta($duplicate_of, 'woocommerce_booking_settings', true);
         $post_title = get_post($value['product_id']);
         $date_check = '';
         if (isset($value['booking'][0]['hidden_date'])) {
             $date_check = date('Y-m-d', strtotime($value['booking'][0]['hidden_date']));
         } else {
             $date_check = '';
         }
         $saved_settings = json_decode(get_option('woocommerce_booking_global_settings'));
         if (isset($saved_settings)) {
             $time_format = $saved_settings->booking_time_format;
         } else {
             $time_format = "12";
         }
         if (isset($value['variation_id'])) {
             $variation_id = $value['variation_id'];
         } else {
             $variation_id = '';
         }
         if (isset($booking_settings['booking_enable_time']) && $booking_settings['booking_enable_time'] == 'on') {
             $type_of_slot = apply_filters('bkap_slot_type', $duplicate_of);
             if ($type_of_slot == 'multiple') {
                 do_action('bkap_validate_cart_items', $value);
             } else {
                 if (isset($value['booking'][0]['time_slot'])) {
                     $time_range = explode("-", $value['booking'][0]['time_slot']);
                     $from_time = date('G:i', strtotime($time_range[0]));
                     if (isset($time_range[1])) {
                         $to_time = date('G:i', strtotime($time_range[1]));
                     } else {
                         $to_time = '';
                     }
                 } else {
                     $to_time = '';
                     $from_time = '';
                 }
                 if ($to_time != '') {
                     $query = "SELECT total_booking, available_booking, start_date FROM `" . $wpdb->prefix . "booking_history`\n\t\t\t\t\t\t\t\t\t\tWHERE post_id = '" . $duplicate_of . "'\n\t\t\t\t\t\t\t\t\t\tAND start_date = '" . $date_check . "'\n\t\t\t\t\t\t\t\t\t\tAND from_time = '" . $from_time . "'\n\t\t\t\t\t\t\t\t\t\tAND to_time = '" . $to_time . "' ";
                     $results = $wpdb->get_results($query);
                 } else {
                     $query = "SELECT total_booking, available_booking, start_date FROM `" . $wpdb->prefix . "booking_history`\n\t\t\t\t\t\t\t\t\tWHERE post_id = '" . $duplicate_of . "'\n\t\t\t\t\t\t\t\t\tAND start_date = '" . $date_check . "'\n\t\t\t\t\t\t\t\t\tAND from_time = '" . $from_time . "'";
                     $results = $wpdb->get_results($query);
                 }
                 if (!$results) {
                     break;
                 } else {
                     if ($value['booking'][0]['time_slot'] != "") {
                         // if current format is 12 hour format, then convert the times to 24 hour format to check in database
                         if ($time_format == '12') {
                             $time_exploded = explode("-", $value['booking'][0]['time_slot']);
                             $from_time = date('h:i A', strtotime($time_exploded[0]));
                             if (isset($time_range[1])) {
                                 $to_time = date('h:i A', strtotime($time_exploded[1]));
                             } else {
                                 $to_time = '';
                             }
                             if ($to_time != '') {
                                 $time_slot_to_display = $from_time . ' - ' . $to_time;
                             } else {
                                 $time_slot_to_display = $from_time;
                             }
                         } else {
                             if ($to_time != '') {
                                 $time_slot_to_display = $from_time . ' - ' . $to_time;
                             } else {
                                 $time_slot_to_display = $from_time;
                             }
                         }
                         if ($results[0]->available_booking > 0 && $results[0]->available_booking < $value['quantity']) {
                             $message = $post_title->post_title . book_t('book.limited-booking-msg1') . $results[0]->available_booking . book_t('book.limited-booking-msg2') . $time_slot_to_display . '.';
                             wc_add_notice($message, $notice_type = 'error');
                         } elseif ($results[0]->total_booking > 0 && $results[0]->available_booking == 0) {
                             $message = book_t('book.no-booking-msg1') . $post_title->post_title . book_t('book.no-booking-msg2') . $time_slot_to_display . book_t('book.no-booking-msg3');
                             wc_add_notice($message, $notice_type = 'error');
                         }
                     }
                 }
             }
         } else {
             if (isset($booking_settings['booking_enable_multiple_day']) && $booking_settings['booking_enable_multiple_day'] == 'on') {
                 $date_checkout = date('d-n-Y', strtotime($value['booking'][0]['hidden_date_checkout']));
                 $date_cheeckin = date('d-n-Y', strtotime($value['booking'][0]['hidden_date']));
                 $order_dates = $this->bkap_betweendays($date_cheeckin, $date_checkout);
                 $todays_date = date('Y-m-d');
                 $query_date = "SELECT DATE_FORMAT(start_date,'%d-%c-%Y') as start_date,DATE_FORMAT(end_date,'%d-%c-%Y') as end_date FROM " . $wpdb->prefix . "booking_history\n\t\t\t\t\t\t\tWHERE start_date >='" . $todays_date . "' AND post_id = '" . $duplicate_of . "'";
                 $results_date = $wpdb->get_results($query_date);
                 //print_r($results_date);
                 $dates_new = array();
                 foreach ($results_date as $k => $v) {
                     $start_date = $v->start_date;
                     $end_date = $v->end_date;
                     $dates = $this->bkap_betweendays($start_date, $end_date);
                     //print_r($dates);
                     $dates_new = array_merge($dates, $dates_new);
                 }
                 $dates_new_arr = array_count_values($dates_new);
                 $lockout = "";
                 if (isset($booking_settings['booking_date_lockout'])) {
                     $lockout = $booking_settings['booking_date_lockout'];
                 }
                 foreach ($order_dates as $k => $v) {
                     if (array_key_exists($v, $dates_new_arr)) {
                         if ($lockout != 0 && $lockout < $dates_new_arr[$v] + $value['quantity']) {
                             $available_tickets = $lockout - $dates_new_arr[$v];
                             $message = $post_title->post_title . book_t('book.limited-booking-date-msg1') . $available_tickets . book_t('book.limited-booking-date-msg2') . $v . '.';
                             wc_add_notice($message, $notice_type = 'error');
                         }
                     } else {
                         if ($lockout != 0 && $lockout < $value['quantity']) {
                             $available_tickets = $lockout;
                             $message = $post_title->post_title . book_t('book.limited-booking-date-msg1') . $available_tickets . book_t('book.limited-booking-date-msg2') . $v . '.';
                             wc_add_notice($message, $notice_type = 'error');
                         }
                     }
                 }
             } else {
                 $query = "SELECT total_booking,available_booking, start_date FROM `" . $wpdb->prefix . "booking_history`\n\t\t\t\t\t\t\t\t\tWHERE post_id = '" . $duplicate_of . "'\n\t\t\t\t\t\t\t\t\tAND start_date = '" . $date_check . "' ";
                 $results = $wpdb->get_results($query);
                 if (!$results) {
                     break;
                 } else {
                     if ($results[0]->available_booking > 0 && $results[0]->available_booking < $value['quantity']) {
                         $message = $post_title->post_title . book_t('book.limited-booking-date-msg1') . $results[0]->available_booking . book_t('book.limited-booking-date-msg2') . $results[0]->start_date . '.';
                         wc_add_notice($message, $notice_type = 'error');
                     } elseif ($results[0]->total_booking > 0 && $results[0]->available_booking == 0) {
                         $message = book_t('book.no-booking-date-msg1') . $post_title->post_title . book_t('book.no-booking-date-msg2') . $results[0]->start_date . book_t('book.no-booking-date-msg3');
                         wc_add_notice($message, $notice_type = 'error');
                     }
                 }
             }
         }
     }
 }