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 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 
<?php
/**
 * Bookings and Appointment Plugin for WooCommerce
 *
 * Class for handling Reuires Confirmation feature
 *
 * @author   Tyche Softwares
 * @package  BKAP/Booking-Confirmation
 * @category Classes
 */
if ( ! class_exists( 'bkap_booking_confirmation' ) ) {

    /**
     * Class for handling Reuires Confirmation feature
     *
     * @class bkap_booking_confirmation
     */
    class bkap_booking_confirmation {

        /**
         * Default constructor
         *
         * @since 2.5
         */
    
        public function __construct() {

            // add checkbox in admin
            add_action( 'bkap_after_purchase_wo_date',                  array( &$this, 'confirmation_checkbox' ), 10, 1 );            
            // save the checkbox value in post meta record
            add_filter( 'bkap_save_product_settings',                   array( &$this, 'save_product_settings' ), 10, 2 );            
            // change the button text product page
            add_filter( 'woocommerce_product_single_add_to_cart_text',  array( &$this, 'change_button_text' ),10, 1 );
            // change the button text on Shop page
            add_filter( 'woocommerce_product_add_to_cart_text',         array( &$this, 'change_button_text' ),10, 1 );
            // Check if Cart contains any product that requires confirmation
            add_filter( 'woocommerce_cart_needs_payment',               array( &$this, 'bkap_cart_requires_confirmation' ), 10, 2 );
            // change the payment gateway at Checkout
            add_filter( 'woocommerce_available_payment_gateways',       array( &$this, 'bkap_remove_payment_methods' ), 10, 1 );
            // Prevent pending being cancelled
            add_filter( 'woocommerce_cancel_unpaid_order',              array( $this, 'bkap_prevent_cancel' ), 10, 2 );
            // Control the my orders actions.
            add_filter( 'woocommerce_my_account_my_orders_actions',     array( $this, 'bkap_my_orders_actions' ), 10, 2 );
            // Add the View Bookings link in Woo->Orders edit orders page
            add_action( 'woocommerce_admin_order_item_headers',         array( $this, 'bkap_link_header' ) );
            add_action( 'woocommerce_admin_order_item_values',          array( $this, 'bkap_link' ), 10, 3 );
            // Ajax calls
            add_action( 'admin_init',                                   array( &$this, 'bkap_confirmations_ajax' ) );
            // Cart Validations
            add_filter( 'bkap_validate_cart_products',                  array( &$this, 'bkap_validate_conflicting_products' ), 10, 2 );
            // Once the payment is completed, order status changes to any of the below, fire these hooks to ensure the booking status is also updated.            
            add_action( 'woocommerce_order_status_processing',          array( &$this, 'bkap_update_booking_status' ), 10, 1 );
            add_action( 'woocommerce_order_status_on-hold',             array( &$this, 'bkap_update_booking_status' ), 10, 1 );
            add_action( 'woocommerce_order_status_completed',           array( &$this, 'bkap_update_booking_status' ), 10, 1 );
            // Remove the booking from the order when it's cancelled
            // Happens only if the booking requires confirmation and the order contains multiple bookings which require confirmation
            add_action( 'bkap_booking_pending-confirmation_to_cancelled', array( $this, 'bkap_remove_cancelled_booking' ) );
        }
        
        /**
         * Ajax Calls for the Confirmation feature.
         * 
         * @hook admin_init
         * @since 2.5
         */

        function bkap_confirmations_ajax() {
            // only logged in users can access the admin side and approve bookings
            // add_action( 'wp_ajax_bkap_save_booking_status', array( &$this, 'bkap_save_booking_status' ) );
        }
        
        /**
         * Add a Requires Confirmation checkbox in the Booking meta box
         * 
         * @param int $product_id - Product ID
         * 
         * @hook bkap_after_purchase_wo_date
         * @since 2.5
         */

        function confirmation_checkbox( $product_id ) {
            
            $booking_settings = bkap_get_post_meta( $product_id );
            ?>
            <div id="requires_confirmation_section" class="booking_options-flex-main">
                        
                <div class="booking_options-flex-child">
                    <label for="bkap_requires_confirmation"><?php _e( 'Requires Confirmation?', 'woocommerce-booking' ); ?></label>
                </div>
                                
                <?php 
                    $date_show = '';
                    if( isset( $booking_settings[ 'booking_confirmation' ] ) && 'on' == $booking_settings[ 'booking_confirmation' ] ) {
                        $requires_confirmation = 'checked';
                    } else {
                        $requires_confirmation = '';
                    }
                ?>
                <div class="booking_options-flex-child">
                    <label class="bkap_switch">
                        <input type="checkbox" name="bkap_requires_confirmation" id="bkap_requires_confirmation" <?php echo $requires_confirmation; ?>>
                        <div class="bkap_slider round"></div>
                    </label>
                </div>
                
                <div class="booking_options-flex-child bkap_help_class">
                    <img class="help_tip" width="16" height="16" data-tip="<?php _e( 'Enable this setting if the booking requires admin approval/confirmation. Payment will not be taken at Checkout', 'woocommerce-booking' ); ?>" src="<?php echo plugins_url() ;?>/woocommerce/assets/images/help.png" />
                </div>
                
            </div>
            
            <script type="text/javascript">
            jQuery(document).ready(function(){
                // when the page loads
                if ( jQuery( "#bkap_requires_confirmation" ).attr( 'checked' ) ) {
                    jQuery( '#booking_purchase_without_date' ).prop( 'disabled', true );
                } else {
                    jQuery( '#booking_purchase_without_date' ).prop( 'disabled', false );
                }
                // when the checkbox is checked/unchecked
                jQuery( "#bkap_requires_confirmation" ).change( function () {
                    if ( jQuery( "#bkap_requires_confirmation" ).attr( 'checked' ) ) {
                        jQuery( "#booking_purchase_without_date" ).attr( 'checked', false );
                        jQuery( '#booking_purchase_without_date' ).prop( 'disabled', true );
                    } else {
                        jQuery( '#booking_purchase_without_date' ).prop( 'disabled', false );
                    }
                });
            });
            </script>
            <?php 
        }
        
        /**
         * Save the Requires Confirmation setting in Booking meta box
         * 
         * @param array $booking_settings - Booking Settings for the product
         * @param int $product_id - product ID
         * @return array $booking_settings - Updated settings
         * 
         * @hook bkap_save_product_settings
         * @since 2.5
         */

        function save_product_settings( $booking_settings, $product_id ) {
            
            $booking_settings[ 'booking_confirmation' ] = '';
            
            if( isset( $_POST[ 'bkap_requires_confirmation' ] ) ) {
                $booking_settings[ 'booking_confirmation' ] = $_POST[ 'bkap_requires_confirmation' ];
            }
            
            return $booking_settings;
        }
        
        /**
         * Modify the Add to cart button text for products that require confirmations
         * 
         * @param string $var - Text for the button
         * @global object $post WP_Post 
         * @hook woocommerce_product_single_add_to_cart_text
         * @since 2.5
         */

        function change_button_text( $var ) {
            global $post;
            // Product ID
            $product_id = $post->ID;
            
            // Return if the prpduct is not bookable.
            $bookable_status = bkap_common::bkap_get_bookable_status( $product_id );
            if( !$bookable_status ) {
                return $var;
            }
            
            $requires_confirmation = bkap_common::bkap_product_requires_confirmation( $product_id );
            
            if( $requires_confirmation ) {
                 $bkap_check_availability_text = get_option( 'bkap_check_availability' );

                if( $bkap_check_availability_text == "" ) {
                    return __( 'Check Availability', 'woocommerce-booking' );
                }else{
                    return __( $bkap_check_availability_text, 'woocommerce-booking' );
                }

            } else {

                $bkap_add_to_cart_text = get_option( 'bkap_add_to_cart' );

                if( $bkap_add_to_cart_text == "" ){
                    return $var;
                }else{
                    return __( $bkap_add_to_cart_text, 'woocommerce-booking' );
                }                
            }            
        }
        
        /**
         * Return true if the cart contains a product that requires confirmation.
         * In this scenario no payment is taken at Checkout
         * 
         * @param boolean $needs_payment
         * @param WC()->cart $cart
         * @return boolean
         * 
         * @hook woocommerce_cart_needs_payment
         * @since 2.5
         */

        function bkap_cart_requires_confirmation( $needs_payment, $cart ) {
            
            if ( ! $needs_payment ) {
                foreach ( $cart->cart_contents as $cart_item ) {
                    $requires_confirmation = bkap_common::bkap_product_requires_confirmation( $cart_item['product_id'] );
                    
                    if( $requires_confirmation ) {
                        $needs_payment = true;
                        break;
                    }
                }
            }
            
            return $needs_payment;            
        }  

        /**
         * Modify Payment Gateways
         * 
         * Remove the existing payment gateways and add the Bookign payment gateway
         * when the Cart contains a product that requires confirmation.
         * 
         * @param array $available_gateways - Array containing all the Payment Gateways
         * @return array $available_gateways - Array containing the Payment Gateways
         * 
         * @hook woocommerce_available_payment_gateways
         * @since 2.5
         */

        function bkap_remove_payment_methods( $available_gateways ) {
        
            $cart_requires_confirmation = bkap_common::bkap_cart_requires_confirmation();
            
            if ( $cart_requires_confirmation ) {
                unset( $available_gateways );
        
                $available_gateways = array();
                $available_gateways['bkap-booking-gateway'] = new BKAP_Payment_Gateway();
            }
        
            return $available_gateways;
        }

        /**
         * Prevent Order Cancellation
         * 
         * Prevent WooCommerce from cancelling an order if the order contains
         * an item that is awaiting booking confirmation once Hold Stock limit is reached.
         * 
         * @param boolean $return
         * @param WC_Order $order
         * @return boolean $return
         * 
         * @hook woocommerce_cancel_unpaid_order
         * @since 2.5
         */

        function bkap_prevent_cancel( $return, $order ) {
            if ( '1' === get_post_meta( $order->get_id(), '_bkap_pending_confirmation', true ) ) {
                return false;
            }
        
            return $return;
        }
        
        /**
         * Hide the Pay button in My Accounts
         * 
         * Hide the Pay button in My Accounts for orders that contain
         * an item that's still awaiting booking confirmation.
         * 
         * @param array $actions - List of Actions for an order on My Account page
         * @param WC_Order $order
         * @global object $wpdb Global wpdb object
         * @return array $actions - List of Actions for an order on My Account page
         * @since 2.5
         */

         function bkap_my_orders_actions( $actions, $order ) {
            global $wpdb;
        
            $order_payment_method = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->payment_method : $order->get_payment_method();
            if ( $order->has_status( 'pending' ) && 'bkap-booking-gateway' === $order_payment_method ) {
                
                $status = array();
                foreach ( $order->get_items() as $order_item_id => $item ) {
                    if ( 'line_item' == $item['type'] ) {
                        
                        $_status = $item[ 'wapbk_booking_status' ];
                        $status[] = $_status;
                 
                    }
                }
        
                if ( in_array( 'pending-confirmation', $status ) && isset( $actions['pay'] ) ) {
                    unset( $actions['pay'] );
                } else if ( in_array( 'cancelled', $status ) && isset( $actions['pay'] ) || count( $status ) == 0 ) {
                    unset( $actions['pay'] );
                }
            }
        
            return $actions;
        }
        
        /**
         * Create a column in WooCommerce->Orders->Edit Orders page 
         * for each item
         * 
         * @hook woocommerce_admin_order_item_headers
         * @since 2.5
         */

        function bkap_link_header() {
           ?><th class="bkap_edit_header">&nbsp;</th><?php
        }
        
        /**
         * Display View Booking Link
         * 
         * Add the View Booking Link for a given item in 
         * WooCommerce->orders Edit Orders
         *  
         * @param WC_Product $_product - Product Details
         * @param WC_Product_Item $item - Item Details
         * @param int $item_id - Item ID
         * @global object $wpdb Global wpdb object
         * 
         * @hook woocommerce_admin_order_item_values
         * @since 2.5
         */

        function bkap_link( $_product, $item, $item_id ) {
            
            global $wpdb;
            
            if ( isset( $_product ) && !empty( $_product ) ) {
                $product_id = $_product->get_id();

                // get the booking settings for the product
                $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
                
                // order ID
                $query_order_id = "SELECT order_id FROM `". $wpdb->prefix."woocommerce_order_items`
                            WHERE order_item_id = %d";
                $get_order_id = $wpdb->get_results( $wpdb->prepare( $query_order_id, $item_id ) );
                
                $order_id = 0;
                if ( isset( $get_order_id ) && is_array( $get_order_id ) && count( $get_order_id ) > 0 ) {
                    $order_id = $get_order_id[0]->order_id;
                }
                
                // get booking posts for the order
                $query_posts = "SELECT ID FROM `" . $wpdb->prefix . "posts`
                                WHERE post_type = %s
                                AND post_parent = %d";
                
                $get_posts = $wpdb->get_results( $wpdb->prepare( $query_posts, 'bkap_booking', $order_id ) );
                
                foreach( $get_posts as $loop_post_id ) {
                
                    $get_item_id = get_post_meta( $loop_post_id->ID, '_bkap_order_item_id', true );
                
                    if ( $get_item_id == $item_id ) {
                        $booking_post_id = $loop_post_id->ID;
                        break;
                    }
                }
                
                // default the variables
                $item_type = '';
                $_status = '';
                if ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) {
                    $_status = ( isset( $item[ 'wapbk_booking_status' ] ) ) ? $item[ 'wapbk_booking_status' ] : ''; // booking status
                    $item_type = $item[ 'type' ]; // line item type
                } else {
                    if ( $item ) {
                         // booking status
                        $meta_data = $item->get_meta_data();
                        foreach( $meta_data as $m_key => $m_value ) {
                            if ( isset( $m_value->key ) && '_wapbk_booking_status' == $m_value->key ) {
                                $_status = $m_value->value;
                                break;
                            }
                        }
                        // line item type
                        $item_type = $item->get_type();
                    }
                }
                
                if ( isset( $booking_post_id ) && $booking_post_id > 0 && ( isset( $item_type ) && 'line_item' == $item_type ) && ( ( isset( $_status ) && '' != $_status ) || ( ! isset( $_status ) ) ) ) {
                    $args = array( 'post' => $booking_post_id, 'action' => 'edit' );
                    ?>
                    <td class="bkap_edit_column">
                        <a href="<?php echo esc_url_raw( add_query_arg( $args, admin_url() . 'post.php' ) ); ?>"><?php _e( 'Edit Booking', 'woocommerce-booking' ); ?></a>
                        <?php do_action( 'bkap_woo_order_item_values', $_product, $item, $item_id )?>
                    </td>
                    <?php 
                } else {
                    echo '<td></td>';
                }
            } else {
                echo '<td></td>';
            }
            
        }
        
        /**
         * Update Item status
         * 
         * This function updates the item booking status. 
         * It is called from the Edit Booking page Save button click
         * 
         * @param integer $item_id - Item ID
         * @param string $_status - New Booking Status
         * @global object $wpdb Global wpdb object
         * 
         * @since 2.5
         */

        static function bkap_save_booking_status( $item_id, $_status ) {
            global $wpdb;
            
            wc_update_order_item_meta( $item_id, '_wapbk_booking_status', $_status );
                
            // get the booking ID using the item ID
            $booking_id = bkap_common::get_booking_id( $item_id );

            // update the booking post status
            if ( $booking_id ) {
                $new_booking = bkap_checkout::get_bkap_booking( $booking_id );
                $new_booking->update_status( $_status );
            }

            // get the order ID
            $order_id = 0;
            $query_order_id = "SELECT order_id FROM `". $wpdb->prefix."woocommerce_order_items`
                                WHERE order_item_id = %d";
            $get_order_id = $wpdb->get_results( $wpdb->prepare( $query_order_id, $item_id ) );
            
            if ( isset( $get_order_id ) && is_array( $get_order_id ) && count( $get_order_id ) > 0 ) {
                $order_id = $get_order_id[0]->order_id;
            }
            
            //create order object
            $order = new WC_Order( $order_id );
            
            // order details
            $order_data = $order->get_items();
            
            $item_value = $order_data[ $item_id ];
                
            // update the booking history tables and GCal
            $confirmation_object = new bkap_booking_confirmation();
            $confirmation_object->update_booking_tables( $_status, $order_id, $item_id, $item_value, $order );
             
            // now check if the product is a bundled product
            // if yes, then we need to update the booking status of all the child products
            $bundled_items = wc_get_order_item_meta( $item_id, '_bundled_items' );
            
            if ( isset( $bundled_items ) && '' != $bundled_items ) {
                $bundle_cart_key = wc_get_order_item_meta( $item_id, '_bundle_cart_key' );
                foreach( $order_data as $o_key => $o_value ) {
                    $bundled_by = wc_get_order_item_meta( $o_key, '_bundled_by' );
                     
                    // check if it is a part of the bundle
                    if ( isset( $bundled_by ) && $bundled_by == $bundle_cart_key ) {
                        // update the booking status
                        wc_update_order_item_meta( $o_key, '_wapbk_booking_status', $_status );
                        // update the booking history tables and GCal
                        $confirmation_object->update_booking_tables( $_status, $order_id, $o_key, $o_value, $order );
                         
                    }
                }
            }
             
            // create an instance of the WC_Emails class , so emails are sent out to customers
            new WC_Emails();
            if ( 'cancelled' == $_status ) {
                do_action( 'bkap_booking_pending-confirmation_to_cancelled_notification', $item_id );
                // remove the item from the order
                do_action( 'bkap_booking_pending-confirmation_to_cancelled', $item_id );
                
            } else if ( 'confirmed' == $_status ) {// if booking has been approved, send email to user
                do_action( 'bkap_booking_confirmed_notification', $item_id );
            }
            
        }
        
        /**
         * Update the plugin tables and GCal for booking status
         * for each Item ID passed
         * 
         * @param string $_status - New Booking Status
         * @param integer $order_id - Order ID
         * @param integer item_id - Item ID
         * @param WC_Product_Item item_value $item_value - Item Details
         * @param WC_Order order - Order Details
         * @global object $wpdb Global wpdb object
         * 
         * @since 3.5
         */

        function update_booking_tables( $_status, $order_id, $item_id, $item_value, $order ) {
        
            global $wpdb;
            // if the booking has been denied, release the bookings for re-allotment
        
            if ( 'cancelled' == $_status ) {
        
                $select_query =   "SELECT booking_id FROM `".$wpdb->prefix."booking_order_history`
                          WHERE order_id= %d";
                $results      =   $wpdb->get_results ( $wpdb->prepare( $select_query, $order_id ) );
        
                $booking_details = array();
        
                foreach ( $results as $key => $value ) {
        
                    $select_query_post   =   "SELECT post_id,start_date, end_date, from_time, to_time FROM `".$wpdb->prefix."booking_history`
                                     WHERE id= %d";
                     
                    $results_post      =   $wpdb->get_results( $wpdb->prepare( $select_query_post, $value->booking_id ) );
        
                    $booking_info = array( 'post_id' => $results_post[0]->post_id,
                        'start_date' => $results_post[0]->start_date,
                        'end_date' => $results_post[0]->end_date,
                        'from_time' => $results_post[0]->from_time,
                        'to_time' => $results_post[0]->to_time
                    );
                    $booking_details[ $value->booking_id ] = $booking_info;
        
                }
        
                foreach ( $booking_details as $booking_id => $booking_data ) {
                    if ( $item_value[ 'product_id' ] == $booking_data['post_id'] ) {
                        // cross check the date and time as well as the product can be added to the cart more than once with different booking details
                        if ( $item_value[ 'wapbk_booking_date' ] == $booking_data[ 'start_date' ] ) {
        
                            $time = $booking_data[ 'from_time' ] . ' - ' . $booking_data[ 'to_time' ];
                            if ( isset( $item_value[ 'wapbk_checkout_date' ] ) && ( $item_value[ 'wapbk_checkout_date' ] == $booking_data[ 'end_date' ] ) ) {
                                $item_booking_id = $booking_id;
                                break;
                            } else if( isset( $item_value[ 'wapbk_time_slot' ] ) && ( $item_value[ 'wapbk_time_slot'] == $time ) ) {
                                $item_booking_id = $booking_id;
                                break;
                            } else {
                                $item_booking_id = $booking_id;
                                break;
                            }
                        }
                    }
                }
        
                bkap_cancel_order::bkap_reallot_item( $item_value, $item_booking_id, $order_id );
        
            } else if ( 'confirmed' == $_status ) {
                 
                require_once plugin_dir_path( __FILE__ ) . '/includes/class.gcal.php';
                // add event in GCal if sync is set to autmated
                $gcal = new BKAP_Gcal();
                 
                $user_id = get_current_user_id();
                if( $gcal->get_api_mode( $user_id, $item_value[ 'product_id' ] ) == "directly" ) {
                     
                    $valid_date = false;
                    if ( isset( $item_value[ 'wapbk_booking_date' ] ) ) {
                        $valid_date = bkap_common::bkap_check_date_set( $item_value[ 'wapbk_booking_date' ] );
                    }
                     
                    if ( $valid_date ) {
                        $event_details = array();
                         
                        $event_details[ 'hidden_booking_date' ] = $item_value[ 'wapbk_booking_date' ];
                         
                        if ( isset( $item_value[ 'wapbk_checkout_date' ] ) && $item_value[ 'wapbk_checkout_date' ] != '' ) {
                            $event_details[ 'hidden_checkout_date' ] = $item_value[ 'wapbk_checkout_date' ];
                        }
                         
                        if ( isset( $item_value[ 'wapbk_time_slot' ] ) && $item_value[ 'wapbk_time_slot' ] != '' ) {
                            $event_details[ 'time_slot' ] = $item_value[ 'wapbk_time_slot' ];
                        }
                         
                    $event_details[ 'billing_email' ] = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->billing_email : $order->get_billing_email();
                        $event_details[ 'billing_first_name' ] = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->billing_first_name : $order->get_billing_first_name();
                        $event_details[ 'billing_last_name' ] = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->billing_last_name : $order->get_billing_last_name();
                        $event_details[ 'billing_address_1' ] = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->billing_address_1 : $order->get_billing_address_1();
                        $event_details[ 'billing_address_2' ] = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->billing_address_2 : $order->get_billing_address_2();
                        $event_details[ 'billing_city' ] = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->billing_city : $order->get_billing_city();
                         
                        $event_details[ 'billing_phone' ] = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->billing_phone : $order->get_billing_phone();
                        $event_details[ 'order_comments' ] = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->customer_note : $order->get_customer_note();
                        $event_details[ 'order_id' ] = $order_id;
                         
                        $shipping_first_name = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->shipping_first_name : $order->get_shipping_first_name();
                        if ( isset( $shipping_first_name ) && $shipping_first_name != '' ) {
                            $event_details[ 'shipping_first_name' ] = $shipping_first_name;
                        }

                        $shipping_last_name = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->shipping_last_name : $order->get_shipping_last_name();
                        if ( isset( $shipping_last_name ) && $shipping_last_name != '' ) {
                            $event_details[ 'shipping_last_name' ] = $shipping_last_name;
                        }
                        
                        $shipping_address_1 = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->shipping_address_1 : $order->get_shipping_address_1(); 
                        if( isset( $shipping_address_1 ) && $shipping_address_1 != '' ) {
                            $event_details[ 'shipping_address_1' ] = $shipping_address_1;
                        }

                        $shipping_address_2 = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->shipping_address_2 : $order->get_shipping_address_2(); 
                        if ( isset( $shipping_address_2 ) && $shipping_address_2 != '' ) {
                            $event_details[ 'shipping_address_2' ] = $shipping_address_2;
                        }
                        
                        $shipping_city = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->shipping_city : $order->get_shipping_city(); 
                        if ( isset( $shipping_city ) && $shipping_city != '' ) {
                            $event_details[ 'shipping_city' ] = $shipping_city;
                        } 
                        
                        $_product = wc_get_product( $item_value[ 'product_id' ] );
                         
                        $post_title = $_product->get_title();
                        $event_details[ 'product_name' ] = $post_title;
                        $event_details[ 'product_qty' ] = $item_value[ 'qty' ];
                         
                        $event_details[ 'product_total' ] = $item_value[ 'line_total' ];
                         
                        // if sync is disabled at the product level, set post_id to 0 to ensure admin settings are taken into consideration
                        $booking_settings = get_post_meta( $item_value[ 'product_id' ], 'woocommerce_booking_settings', true );
                        $post_id = $item_value[ 'product_id' ];
                        if ( ( ! isset( $booking_settings[ 'product_sync_integration_mode' ] ) ) || ( isset( $booking_settings[ 'product_sync_integration_mode' ] ) && 'disabled' == $booking_settings[ 'product_sync_integration_mode' ] ) ) {
                            $post_id = 0;
                        }
                         
                        $gcal->insert_event( $event_details, $item_id, $user_id, $post_id, false );
                         
                        // add an order note, mentioning an event has been created for the item
                        $order_note = __( "Booking_details for $post_title have been exported to the Google Calendar", 'woocommerce-booking' );
                        $order->add_order_note( $order_note );
                    }
                }
            }       
        }
        
        /**
         * Validate bookable products 
         * 
         * This function displays a notice and empties the cart if the cart contains
         * any products that conflict with the new product being added. 
         * 
         * @param array $_POST
         * @param int $product_id - Product ID
         * @return string
         * 
         * @hook bkap_validate_cart_products
         * @since 2.5
         */

        function bkap_validate_conflicting_products( $POST, $product_id ) {
            
            $quantity_check_pass = 'yes';
            // check if the product being added requires confirmation
            $product_requires_confirmation = bkap_common::bkap_product_requires_confirmation( $product_id );
            
            // check if the cart contains a product that requires confirmation
            $cart_requires_confirmation = bkap_common::bkap_cart_requires_confirmation();
            
            $validation_status = 'warn_modify_yes';
            
            switch ( $validation_status ) {
                case 'warn_modify_yes':
                    $conflict = 'NO';
                    
                    if ( count( WC()->cart->cart_contents ) > 0 ) {
                    // if product requires confirmation and cart contains product that does not
                        if ( $product_requires_confirmation && ! $cart_requires_confirmation ) {
                            $conflict = 'YES';
                        }
                        // if product does not need confirmation and cart contains a product that does
                        if ( ! $product_requires_confirmation && $cart_requires_confirmation ) {
                            $conflict = 'YES';
                        }
                        // if conflict
                        if ( 'YES' == $conflict ) {
                            // remove existing products
                            WC()->cart->empty_cart();
                            
                            // add a notice
                            $message = bkap_get_book_t( 'book.conflicting-products' );
                            wc_add_notice( __( $message, 'woocommerce-booking' ), $notice_type = 'notice' );
                        }
                    }
                    break;
            }
            
            return $quantity_check_pass; 
        }

        /**
         * Update Booking status to paid
         * 
         * Updates the Booking status to paid to ensure they do not remain
         * in the Unpaid section in Booking->View Bookings
         * 
         * @param int $order_id
         * 
         * @hook woocommerce_order_status_processing
         *       woocommerce_order_status_on-hold
         *       woocommerce_order_status_completed
         * @since 2.5
         */

        function bkap_update_booking_status ( $order_id ) {
            
            $order_obj    =   new WC_order( $order_id );
            $order_items  =   $order_obj->get_items();
            foreach( $order_items as $item_key => $item_value ) {

                $booking_status = wc_get_order_item_meta( $item_key, '_wapbk_booking_status' );
                
                if ( isset( $booking_status ) && 'confirmed' == $booking_status ) {
                    wc_update_order_item_meta( $item_key, '_wapbk_booking_status', 'paid' );
                    
                    // get the booking ID using the item ID
                    $booking_id = bkap_common::get_booking_id( $item_key );

                    // update the booking post status
                    if ( $booking_id ) {
                        $new_booking = bkap_checkout::get_bkap_booking( $booking_id );
                        $new_booking->update_status( 'paid' );
                    }
                }
            }
        }
        
        /**
         * Remove an item from the order if the booking is not approved/rejected/cancelled
         * by the admin
         * 
         * @param int $item_id - Item ID
         * @global object $wpdb Global wpdb object
         * 
         * @since 2.5
         */
        
        function bkap_remove_cancelled_booking( $item_id ) {
            global $wpdb;
            
            $booking  = bkap_common::get_bkap_booking( $item_id );
            $order = new WC_order( $booking->order_id );
            $bookings = array();
            
            if ( ! empty ( $order ) && is_array( $order->get_items() ) ) {
                foreach ( $order->get_items() as $order_item_id => $item ) {
                    if ( $order_item_id == $item_id ) {
                        wc_delete_order_item( $order_item_id );
                        $order->calculate_totals();
                        $order->add_order_note( sprintf( __( 'The product %s has been removed from the order because the booking cannot be confirmed.', 'woocommerce-booking' ), $item['name'] ) );
                    }
                }
            }
        }
    } 
}
$bkap_booking_confirmation = new bkap_booking_confirmation(); 
?>
API documentation generated by ApiGen