Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hook Reference
  • Docs Home

Packages

  • BKAP
    • Addons
    • Admin-Bookings
    • Attribute-Lockout
    • Block-Pricing
    • Booking-Box
    • Booking-Confirmation
    • Booking-Process
    • Cancel-Order
    • Cart
    • Checkout-Process
    • Core
    • Date-Time
    • Emails
    • Functions
    • Global-Function
    • Global-Settings
    • Google-Calendar-Sync
    • ICS
    • Import-Booking
    • Labels
    • License
    • Menus
    • Meta-Boxes
    • Reschedule
    • Resources
    • Search-Widget
    • Special-Price
    • Uninstall
    • Validation
    • Variation-Lockout
    • Vendors
      • Dokan
      • WC-Vendors
    • View-Bookings
  • None

Classes

  • bkap_addon_compatibility_class
  • bkap_admin_bookings
  • bkap_approve_booking
  • bkap_attributes
  • bkap_block_booking
  • BKAP_Booking
  • bkap_booking_box_class
  • bkap_booking_confirmation
  • bkap_booking_process
  • BKAP_Bookings_View
  • bkap_calendar_sync
  • bkap_cancel_order
  • bkap_cart
  • bkap_checkout
  • bkap_common
  • BKAP_Customer_Meta_Box
  • BKAP_Details_Meta_Box
  • bkap_dokan_calendar_class
  • bkap_dokan_class
  • bkap_dokan_orders_class
  • bkap_dokan_products_class
  • bkap_edit_bookings_class
  • BKAP_Email_Booking_Cancelled
  • BKAP_Email_Booking_Confirmed
  • BKAP_Email_Booking_Rescheduled_Admin
  • BKAP_Email_Imported_Event
  • BKAP_Email_Manager
  • BKAP_Email_New_Booking
  • BKAP_Gcal
  • BKAP_Gcal_Event
  • bkap_gcal_sync_settings
  • bkap_global_settings
  • BKAP_Google_Events_View
  • bkap_ics
  • bkap_license
  • bkap_load_scripts_class
  • BKAP_OPC_Addon
  • BKAP_Payment_Gateway
  • BKAP_Product_Resource
  • bkap_rescheduled_order_class
  • BKAP_Resource_Details_Meta_Box
  • BKAP_Save_Meta_Box
  • bkap_special_booking_price
  • bkap_timeslot_price
  • bkap_validation
  • bkap_variations
  • BKAP_Vendors
  • bkap_wc_vendors
  • bkap_wcv_product
  • Booking_Information_In_Voucher_Template
  • Class_Bkap_Product_Resource
  • Custom_WooCommerce_Widget_Product_Search
  • EDD_BOOK_Plugin_Updater
  • global_menu
  • import_bookings
  • product_gcal_settings
  • view_bookings
  • WAPBK_Import_Bookings_Table
  • WAPBK_View_Bookings_Table
  • woocommerce_booking

Functions

  • addDayswithdate
  • bkap_400_create_meta
  • bkap_400_recurring_data
  • bkap_400_update_db_tab
  • bkap_400_update_enable_specific
  • bkap_400_update_enable_week_blocking
  • bkap_400_update_holidays
  • bkap_400_update_ranges
  • bkap_400_update_recurring_lockout
  • bkap_400_update_settings
  • bkap_400_update_special
  • bkap_400_update_specific
  • bkap_410_create_meta
  • bkap_410_update_block_pricing_option_values_in_new_way
  • bkap_410_update_fixed_blocks
  • bkap_410_update_price_ranges
  • bkap_410_update_settings
  • bkap_420_add_gcal_posts
  • bkap_420_add_gcal_tour_posts
  • bkap_420_gcal_batch_size
  • bkap_420_gcal_meta
  • bkap_420_update_settings
  • bkap_add_days_to_date
  • bkap_array_of_given_date_range
  • bkap_booked_resources
  • bkap_booking_posts_for_resource
  • bkap_calculate_bookings_for_resource
  • bkap_check_booking_available
  • bkap_check_day_booking_available
  • bkap_check_for_max_date
  • bkap_check_for_min_date
  • bkap_check_holiday
  • bkap_check_in_custom_holiday_range
  • bkap_check_resource_available
  • bkap_check_resource_booked_in_date_range
  • bkap_db_420_1
  • bkap_db_420_2
  • bkap_delete_event_from_gcal
  • bkap_delete_folder_and_files
  • bkap_delete_options
  • bkap_delete_tables
  • bkap_find_matching_product_variation
  • bkap_generate_bookable_data
  • bkap_get_book_arrays
  • bkap_get_book_t
  • bkap_get_booked
  • bkap_get_booked_checkout
  • bkap_get_custom_posts
  • bkap_get_dateToCal
  • bkap_get_day_between_Week
  • bkap_get_db_count
  • bkap_get_escapeString
  • bkap_get_lockout
  • bkap_get_month_range
  • bkap_get_post_meta
  • bkap_get_posted_availability
  • bkap_get_total_timeslot_maximum_booking
  • bkap_get_week_range
  • bkap_import_events_cron
  • bkap_insert_event_to_gcal
  • bkap_manual_db_update_v420
  • bkap_orderby_date_key
  • bkap_save_resources
  • bkap_sort_by_from_time
  • bkap_update_po_file
  • bkap_vendors_get_bookings
  • bkap_widgets_init
  • bkapSortByPriority
  • calback_bkap_max_date
  • check_in_fixed_block_booking
  • check_in_range
  • check_in_range_abp
  • check_in_range_holidays
  • check_in_range_weekdays
  • create_posts
  • get_availability_for_date
  • get_booked_multiple
  • get_bookings_for_date
  • get_bookings_for_range
  • get_date_lockout
  • get_slot_availability
  • get_slot_lockout
  • get_standard_decimal_thousand_separator_price
  • is_booking_active
  • woocommerce_bkap_add_cron_schedule
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 
<?php
/**
 * Booking & Appointment Plugin for WooCommerce
 *
 * Class for Handling the timeslot pricing and to add hidden data on front end product page of the date and time booking product
 *
 * @author      Tyche Softwares
 * @category    Core
 * @package     BKAP/Date-Time
 * @version     2.0
 */

if ( !class_exists( 'bkap_timeslot_price' ) ) {

    /**
     * Class for price caluculations for the timeslots
     * @class bkap_timeslot_price
     */
    
    class bkap_timeslot_price {
        
        /**
         * Default constructor
         *
         * @since 2.0
         */

        public function __construct() {
            
            // Print hidden fields
            add_action( 'bkap_print_hidden_fields',          array( &$this, 'timeslot_hidden_fields' ), 10, 1 );
            // Display updated price on the product page
            add_action( 'bkap_display_updated_addon_price',  array( &$this, 'timeslot_display_updated_price' ), 3, 5 );
        }

        /**
         * This function will add hidden field for the timeslot price 
         *
         * @since 2.0
         * @hook bkap_print_hidden_fields
         * @param int $product_id Product Id
         */
        
        function timeslot_hidden_fields( $product_id ) {
            $variable_timeslot_price = bkap_timeslot_price::get_timeslot_variable_price( $product_id );
            print( '<input type="hidden" id="wapbk_hidden_variable_timeslot_price" name="wapbk_hidden_variable_timeslot_price" value="' . $variable_timeslot_price . '">' );
        }

        /**
         * This function will calculate if the timeslot having price or not 
         *
         * @since 2.0
         * @param int $product_id  Product Id
         * @return string $variable_timeslot_price if timeslot having price then returns Yes else No.
         */
        
        function get_timeslot_variable_price( $product_id ) {
            $slot_price          = array();
            $booking_settings    = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
            if ( isset( $booking_settings[ 'booking_enable_time' ] ) && $booking_settings[ 'booking_enable_time' ] == 'on' ) {
                $time_slot_arr = ( isset( $booking_settings[ 'booking_time_settings' ] ) ) ? $booking_settings[ 'booking_time_settings' ] : array();
                
                if ( is_array( $time_slot_arr ) && count( $time_slot_arr ) > 0 ) {
                    foreach ( $time_slot_arr as $key => $value ) {
                        foreach ( $value as $k => $v ) {
                            if ( isset( $v[ 'slot_price' ] ) && $v[ 'slot_price' ] > 0 ) {
                                $slot_price[] = $v[ 'slot_price' ];
                            }
                        }
                    }
                }
            }
            
            $variable_timeslot_price = 'no';
            if( count( $slot_price ) > 0 ) {
                $variable_timeslot_price = 'yes';
            }
            return $variable_timeslot_price;
        }

        /**
         * This function will display the price of selected date and time
         *
         * @since 2.0
         * @hook bkap_display_updated_addon_price
         * @param int $product_id Product ID
         * @param string $booking_date Date
         * @param int $variation_id Variation ID
         * @param int $gf_options Option price from Gravity Forms
         * @param int $resource_id Resource ID
         */
        
        function timeslot_display_updated_price( $product_id, $booking_date, $variation_id, $gf_options = 0, $resource_id = 0 ) {
            $global_settings     = json_decode( get_option( 'woocommerce_booking_global_settings' ) );
            // product type
            $_product            = wc_get_product( $product_id );
            $product_type        = $_product->get_type();
            // Time slot
            $time_slot           = '';
            
            if ( isset( $_POST[ 'timeslot_value' ] ) ) {
                $time_slot = $_POST[ 'timeslot_value' ];
            }
            
            // If resource is attached to the product then calculating resource price and adding it to final price.
            $resource_price = 0;
            if ( $resource_id != 0 ) {
                $resource       = new BKAP_Product_Resource( $resource_id, $product_id );
                $resource_price = $resource->get_base_cost();
            }
            
            if ( $product_type == 'grouped' ) {
                $currency_symbol    = get_woocommerce_currency_symbol();
                $has_children       = $price_str = "";
                $price_arr          = array();
                
                if ( $_product->has_child() ) {
                    $has_children = "yes";
                    $child_ids = $_product->get_children();
                }
                
                $quantity_grp_str = $_POST[ 'quantity' ];
                $quantity_array = explode( ",", $quantity_grp_str );
                $i              = 0;
                $raw_price_str  = '';
                foreach ( $child_ids as $k => $v ) {
                    $child_product         = wc_get_product( $v );
                    $product_type_child    = $child_product->get_type();
                    $time_slot_price       = $this->get_price( $v, 0, $product_type_child, $booking_date, $time_slot, 'product' );
                    $final_price           = $time_slot_price * $quantity_array[ $i ];
                    $raw_price             = $final_price;
                    
                    if ( function_exists( 'icl_object_id' ) ) {
                        global $woocommerce_wpml;
                        // Multi currency is enabled
                        if ( isset( $woocommerce_wpml->settings[ 'enable_multi_currency' ] ) && $woocommerce_wpml->settings[ 'enable_multi_currency' ] == '2' ) {
                            $custom_post = bkap_common::bkap_get_custom_post( $v, 0, $product_type );
                            if( $custom_post == 0 ) {
                                $raw_price = apply_filters( 'wcml_raw_price_amount', $final_price );
                                $final_price = $raw_price;
                            }
                        } 
                    } 
                    $wc_price_args = bkap_common::get_currency_args();
                    $final_price = wc_price( $final_price, $wc_price_args );
                    
                    $raw_price_str .= $v . ":" . $raw_price . ",";
                    $price_str .= $child_product->get_title() . ": " . $final_price . "<br>";
                    $i++;
                }
                $time_slot_price = $price_str;
            } else {
                $time_slot_price = "";
                if( $time_slot != "" ){
                    $time_slot_price = $this->get_price( $product_id, $variation_id, $product_type, $booking_date, $time_slot, 'product' ); 
                }
            }
            
            if ( ( $time_slot_price == "" ) && ( isset( $_POST[ 'special_booking_price' ] ) && $_POST[ 'special_booking_price' ] != "" ) ){
                $time_slot_price = $_POST[ 'special_booking_price' ];
                $raw_price_str = $_POST[ 'grouped_raw_price' ];
            } elseif ( $time_slot_price === "" ) {
                $time_slot_price = bkap_common::bkap_get_price( $product_id, $variation_id, $product_type );
            }                       

            $time_slot_price = $time_slot_price + $resource_price;
            
            if( ( function_exists( 'is_bkap_seasonal_active' ) && is_bkap_seasonal_active() ) || 
                ( function_exists( 'is_bkap_deposits_active' ) && is_bkap_deposits_active() ) || 
                ( function_exists( 'is_bkap_multi_time_active' ) && is_bkap_multi_time_active() ) ) {

                $_POST[ 'price' ] = $time_slot_price;
            } else {
                if ( $product_type != 'grouped' ) {
                    if ( isset( $_POST[ 'quantity' ] ) && $_POST[ 'quantity' ] != 0 ) {
                        $time_slot_price = $time_slot_price * $_POST[ 'quantity' ];
                    }
                    
                    if ( isset( $global_settings->enable_rounding ) && $global_settings->enable_rounding == "on" ) {
                        $time_slot_price = round( $time_slot_price );
                    }
                    
                    // Save the actual Bookable amount, as a raw amount
                    // If Multi currency is enabled, convert the amount before saving it
                    $total_price = $time_slot_price;
                    if ( function_exists( 'icl_object_id' ) ) {
                        $custom_post = bkap_common::bkap_get_custom_post( $product_id, $variation_id, $product_type );
                        if( $custom_post == 1 ) {
                            $total_price = $time_slot_price;
                        } else if( $custom_post == 0 ) {
                            $total_price = apply_filters( 'wcml_raw_price_amount', $time_slot_price );
                        }
                    }
                    
                    print( 'jQuery( "#total_price_calculated" ).val(' . $total_price . ');' );
                    // save the price in a hidden field to be used later
                    print( 'jQuery( "#bkap_price_charged" ).val(' . $total_price. ');' );

                    // if gf options are enable .. commented since we no longer need to display price below Booking Box
                    /*if ( isset( $gf_options ) && $gf_options > 0 ) {
                        $total_price += $gf_options;
                    }*/
                    
                    // format the price
                    $wc_price_args = bkap_common::get_currency_args();
                    $formatted_price = wc_price( $total_price, $wc_price_args );
                    
                } else {
                    $formatted_price = $time_slot_price;
                    print( 'jQuery( "#total_price_calculated" ).val("' . addslashes( $raw_price_str ) . '");' );
                    // save the price in a hidden field to be used later
                    print( 'jQuery( "#bkap_price_charged" ).val("' . addslashes( $raw_price_str ) . '");' );
                } 

                if ( isset( $total_price ) && 'bundle' == $product_type ) {
                    $bundle_price = bkap_common::get_bundle_price( $total_price, $product_id, $variation_id );

                    $formatted_price = wc_price( $bundle_price, $wc_price_args );
                }

                if ( 'composite' === $product_type ) {
                    $composite_price = bkap_common::get_composite_price( $total_price, $product_id, $variation_id );

                    $formatted_price = wc_price( $composite_price, $wc_price_args );
                }

                // display the price on the front end product page
                $display_price = get_option( 'book_price-label' ) . ' ' . $formatted_price;
                print( 'jQuery( "#bkap_price" ).html( "' . addslashes( $display_price ) . '");' );

                die();
            }
        }
        
/*      function add_cart_item( $cart_item ) {
            global $wpdb;
            $product_type = 'simple';           
            if ( $cart_item[ 'variation_id' ] != '' ) {
                $product_type = 'variable';
            }           
            $price = bkap_common::bkap_get_price( $cart_item['product_id'], $cart_item['variation_id'], $product_type );
            
            // Adjust price if addons are set
            if ( isset( $cart_item['bkap_booking'] ) ) {
                $extra_cost = 0;
                foreach ( $cart_item['bkap_booking'] as $addon ) {
                    if ( isset( $addon['price'] ) && $addon['price'] > 0 ) {
                        $extra_cost += $addon['price'];
                    }
                };
                $extra_cost = $extra_cost - $price;
                $cart_item[ 'data' ]->adjust_price( $extra_cost );
            }       
            
            return $cart_item;
        } */

        /**
         * This function will return the price of the selected timeslot.
         *
         * @since 4.2.0
         * @param int $product_id Product ID
         * @param int $variation_id Variation ID
         * @param string $product_type Type of the Product
         * @param string $booking_date Date
         * @param string $called_from Called from product
         * @return int $time_slot_price Price of the timeslot
         */
        
        function get_price( $product_id, $variation_id, $product_type, $booking_date, $time_slot, $called_from ) {
            // set the slot price as the product base price
            $time_slot_price = $time_slot_price_total = "";
            
            if( isset( $_POST[ 'special_booking_price' ] ) && $_POST[ 'special_booking_price' ] != "" ) {
                $time_slot_price = $_POST[ 'special_booking_price' ];
                // if bundled product then we need to add the individually charged child prices as well
                $_product = wc_get_product( $product_id );
                $prd_type = $_product->get_type();
                if ( isset( $prd_type ) && 'bundle' == $prd_type ) {
                    $bundle_total = bkap_common::bkap_get_price( $product_id, $variation_id, $prd_type );
                    // product price
                    $regular_price = get_post_meta( $product_id, '_regular_price', true );
                    $sale_price = get_post_meta( $product_id, '_sale_price', true );

                    if( !isset( $sale_price ) || $sale_price == '' || $sale_price == 0 ) {
                        $prd_price          =   $regular_price;
                    } else {
                        $prd_price          =   $sale_price;
                    }
                
                    // now subtract the product price and add the special price
                    $bundle_total -= $prd_price;
                    $time_slot_price += $bundle_total;
                
                }
            } else {
                $time_slot_price = bkap_common::bkap_get_price( $product_id, $variation_id, $product_type, $booking_date );
            }

            $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
            if ( $time_slot != '' ) {
                // Check if multiple time slots are enabled
                $seperator_pos = strpos( $time_slot, "," );
                
                if ( isset( $seperator_pos ) && $seperator_pos != "" ) {
                    $time_slot_array = explode( ",", $time_slot );
                } else {
                    $time_slot_array   = array();
                    $time_slot_array[] = $time_slot;
                }
                
                for ( $i = 0; $i < count( $time_slot_array ); $i++ ) {
                    // split the time slot into from and to time
                    $timeslot_explode      = explode( '-', $time_slot_array[ $i ] );
                    $timeslot_explode[0]   = date( 'G:i', strtotime( $timeslot_explode[0] ) );
                    
                    if ( isset( $timeslot_explode[1] ) && $timeslot_explode[1] != '' ) {
                        $timeslot_explode[1] = date( 'G:i', strtotime( $timeslot_explode[1] ) );
                    }
                    // split frm hrs in hrs and min
                    $from_hrs  = explode( ':', $timeslot_explode[0] );
                    // similarly for to time, but first default it to 0:00, so it works for open ended time slots as well
                    $to_hrs    = array(
                            '0' => '0',
                            '1' => '00');
                    
                    if ( isset( $timeslot_explode[1] ) && $timeslot_explode[1] != '' ) {
                        $to_hrs = explode( ':', $timeslot_explode[1] );
                    }
                    
                    if ( isset( $booking_settings[ 'booking_time_settings' ] ) && count( $booking_settings[ 'booking_time_settings' ] ) > 0 ) {                     
                        
                        // match the booking date as specific overrides recurring
                        $booking_date_to_check = date( 'j-n-Y', strtotime( $booking_date ) );
                        if ( array_key_exists( $booking_date_to_check, $booking_settings[ 'booking_time_settings' ] ) ) {
                            foreach ($booking_settings[ 'booking_time_settings' ] as $key => $value ) {
                                if ( $key == $booking_date_to_check ) {
                                    foreach ( $value as $k => $v ) {
                                        $price = 0;
                                        // match the time slot
                                        if ( ( intval( $from_hrs[0] ) == intval( $v['from_slot_hrs'] ) ) && ( intval( $from_hrs[1] ) == intval( $v['from_slot_min'] ) ) && ( intval( $to_hrs[0] ) == intval( $v['to_slot_hrs'] ) ) && ( intval( $to_hrs[1] ) == intval( $v['to_slot_min'] ) ) ) {
                                            // fetch and save the price
                                            if ( isset( $v[ 'slot_price' ] ) && $v[ 'slot_price' ] != '' ) {
                                                $price = $v[ 'slot_price' ];
                                                if ( isset( $called_from ) && $called_from == 'cart' ) {
                                                    $price = apply_filters( 'wcml_raw_price_amount', $v[ 'slot_price' ] );
                                                }
                                                $time_slot_price_total += $price;
                                            } else {
                                                $time_slot_price_total += $time_slot_price;
                                            }
                                        }
                                    }
                                }
                            }
                        } else {
                            // Get the weekday
                            $weekday            =   date( 'w', strtotime( $booking_date ) );
                            $booking_weekday    =   'booking_weekday_' . $weekday;
                            foreach ($booking_settings[ 'booking_time_settings' ] as $key => $value ) {
                                //match the weekday
                                if ( $key == $booking_weekday ) {
                                    foreach ( $value as $k => $v ) {
                                        $price = "";
                                        // match the time slot
                                        if ( ( intval( $from_hrs[0] ) == intval( $v[ 'from_slot_hrs' ] ) ) && ( intval( $from_hrs[1] ) == intval( $v[ 'from_slot_min' ] ) ) && ( intval( $to_hrs[0] ) == intval( $v[ 'to_slot_hrs' ] ) ) && ( intval( $to_hrs[1] ) == intval( $v[ 'to_slot_min' ] ) ) ) {
                                            // fetch and save the price
                                            if ( isset( $v[ 'slot_price' ] ) && $v[ 'slot_price' ] != '' ) {
                                                $price = $v['slot_price'];
                                                if ( isset( $called_from ) && $called_from == 'cart' ) {
                                                    $price = apply_filters( 'wcml_raw_price_amount', $v['slot_price'] );
                                                }
                                                $time_slot_price_total += $price;
                                            } else {
                                                $time_slot_price_total += $time_slot_price;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if ( $time_slot_price_total !== "" ) {
                    $time_slot_price = $time_slot_price_total;
                    $time_slot_price = $time_slot_price / count($time_slot_array);
                }
            }
            return $time_slot_price;
        }
    }
}
$bkap_timeslot_price = new bkap_timeslot_price();
API documentation generated by ApiGen