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  775  776  777  778  779  780  781  782  783  784  785  786  787  788  789  790  791  792  793  794  795  796  797  798  799  800  801  802  803  804  805  806  807  808  809  810  811  812  813  814  815  816  817  818  819  820  821  822  823  824  825  826  827  828  829  830  831  832  833  834  835  836  837  838  839  840  841  842  843  844  845  846  847  848  849  850  851  852  853  854  855  856  857  858  859  860  861  862  863  864  865  866  867  868  869  870  871  872  873  874  875  876  877  878  879  880  881  882  883  884  885  886  887  888  889  890  891  892  893  894  895  896  897  898  899  900  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930  931  932  933  934  935  936  937  938  939  940  941  942  943  944  945  946  947  948  949  950  951  952  953  954  955  956  957  958  959  960  961  962  963  964  965  966  967  968  969  970  971  972  973  974  975  976  977  978  979  980  981  982  983  984  985  986  987  988  989  990  991  992  993  994  995  996  997  998  999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 
<?php

/**
 * Bookings and Appointment Plugin for WooCommerce
 *
 * Class for handling Bookings of an order when an Order or Booking is cancelled
 *
 * @author   Tyche Softwares
 * @package  BKAP/Cancel-Order
 * @category Classes
 */

include_once( 'bkap-common.php' );
include_once( 'lang.php' );

/**
 * Class for listing the bookings on respective pages
 */
class bkap_cancel_order{

    /**
     * This function will add cancel order button on the MY ACCOUNT  page. 
     * For cancelling the order.
     * 
     * @hook woocommerce_my_account_my_orders_actions
     * 
     * @param WC_Order $order Order Object
     * @param mixed $action Action performed
     * 
     * @return WC_Order WooCommerce Order Object
     * 
     * @since 1.7.0
     */

    public static function bkap_get_add_cancel_button( $order, $action ){
        $myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
        
        if ( $myaccount_page_id ) {
            $myaccount_page_url = get_permalink( $myaccount_page_id );
        }
            
        $action_id = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $action->id : $action->get_id();
        $action_status = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $action->status : $action->get_status();
        
        if ( isset( $_GET['order_id'] ) &&  $_GET['order_id'] == $action_id && $_GET['cancel_order'] == "yes" ) {
            $order_obj = new WC_Order( $action_id );
            $order_obj->update_status( "cancelled" );
            print('<script type="text/javascript">
                    location.href="'.$myaccount_page_url.'";
                   </script>');
        }
        
        if ( $action_status != "cancelled" && $action_status != "completed" && $action_status != "refunded") {
            $order['cancel'] = array( "url"   => apply_filters( 'woocommerce_get_cancel_order_url', add_query_arg( 'order_id', $action_id )."&cancel_order=yes"),
                                      "name"  => __( "Cancel", "woocommerce-booking" ) );
        }
        
        return $order;
    }
    
    /**
     * This function frees up the booking dates and/or time
     * for all the items in an order when the order is trashed
     * without cancelling or refunding it.
     * 
     * @hook wp_trash_post
     * 
     * @param string|int $post_id Order ID whose booking needs to be trashed
     * 
     * @since 1.7.0
     */

    public static function bkap_trash_order( $post_id ) {
        $post_obj = get_post( $post_id );
         
        // array of all the  order statuses for which the bookings do not need to be freed up
        $status = array( 'wc-cancelled', 'wc-refunded' );
         
        if ( 'shop_order' == $post_obj->post_type && ( !in_array( $post_obj->post_status, $status ) ) ) {
            
            // trash the booking posts as well
            $order    = new WC_Order( $post_id );
            foreach ( $order->get_items() as $order_item_id => $item ) {
                if ( 'line_item' == $item['type'] ) {
                    // get the booking ID for each item
                    $booking_id = bkap_common::get_booking_id( $order_item_id );
                    wp_trash_post( $booking_id );
                }
            }
             
            bkap_cancel_order::bkap_woocommerce_cancel_order( $post_id );
        }
        
        if ( 'bkap_booking' == $post_obj->post_type ) {
            $booking_id = $post_obj->ID;
            woocommerce_booking::bkap_delete_booking( $booking_id );
        }
    }

    /**
     * Untrash the order and restore the bookings
     * 
     * @hook untrash_post
     * 
     * @param string|int $post_id Order ID whose booking needs to be untrashed
     * 
     * @since 1.7.0
     */

    public static function bkap_untrash_order( $post_id ) {
        $post_obj = get_post( $post_id );
        
        if ( 'shop_order' == $post_obj->post_type && ( 'wc-cancelled' != $post_obj->post_status || 'wc-refunded' != $post_obj->post_status ) ) {
            
            // untrash the booking posts as well
            $order    = new WC_Order( $post_id );
            foreach ( $order->get_items() as $order_item_id => $item ) {
                if ( 'line_item' == $item['type'] ) {
                    // get the booking ID for each item
                    $booking_id = bkap_common::get_booking_id( $order_item_id );
                    wp_untrash_post( $booking_id );
                }
            }
             
            bkap_cancel_order::bkap_woocommerce_restore_bookings( $post_id, 'trashed', $post_obj->post_status );
        }
    }
    
    /**
     * Restore bookings once an Order is untrashed
     * 
     * @param string|int $order_id Order ID
     * @param string $old_status Current Status active
     * @param string $new_status New Status to be changed
     * 
     * @since 1.7.0
     * 
     * @globals mixed $wpdb global variable
     * @globals WP_Post $post
     */

    public static function bkap_woocommerce_restore_bookings( $order_id, $old_status, $new_status ) {
        if( ( $old_status == 'cancelled' && ( $new_status != 'cancelled' || $new_status != 'refunded' ) ) || ( $old_status == 'refunded' && ( $new_status != 'cancelled' || $new_status != 'refunded' ) ) 
            || ( $old_status == 'trashed' && ( $new_status != 'cancelled' || $new_status != 'refunded' ) ) ) {
            global $wpdb, $post;
            $order_obj    =   new WC_order( $order_id );
            $order_items  =   $order_obj->get_items();
            foreach( $order_items as $item_key => $item_value ) {
                $product_id      =   bkap_common::bkap_get_product_id( $item_value[ 'product_id' ] );
                $booking_data = array();
                if( isset( $item_value[ get_option( 'book_item-meta-date' ) ] ) ) {
                    $booking_data[ 'date' ] = $item_value[ get_option( 'book_item-meta-date' ) ];
                }
                 
                if( isset( $item_value[ 'wapbk_booking_date' ] ) ) {
                    $booking_data[ 'hidden_date' ] = $item_value[ 'wapbk_booking_date' ];
                }
                
                if( isset( $item_value[ 'wapbk_checkout_date' ] ) ) {
                    $booking_data[ 'hidden_date_checkout' ] = $item_value[ 'wapbk_checkout_date' ];
                }
                
                if( isset( $item_value[ get_option( 'book_item-meta-time' ) ] ) ) {
                    $booking_data[ 'time_slot' ] = $item_value[ get_option( 'book_item-meta-time' ) ];
                }
                
                $_product  =   wc_get_product( $product_id );
                $parent_id = 0;
                if ( is_bool( $_product ) === false ) {
                    $parent_id = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $_product->get_parent() : bkap_common::bkap_get_parent_id( $product_id );
                }
                
                $details = bkap_checkout::bkap_update_lockout(  $order_id, $product_id, $parent_id, $item_value[ 'qty' ], $booking_data, 'admin' );
                // update the global time slot lockout
                if( isset( $item_value[ get_option( 'book_item-meta-time' ) ] ) && $item_value[ get_option( 'book_item-meta-time' ) ] != "" ) {
                    bkap_checkout::bkap_update_global_lockout( $product_id, $item_value[ 'qty' ], $details, $booking_data );
                }
                
                // get booking post ID
                $booking_post = bkap_common::get_booking_id( $item_key );
                // update the booking post status
                if ( $booking_post ) {
                    $new_booking = bkap_checkout::get_bkap_booking( $booking_post );
                    $status = wc_get_order_item_meta( $item_key, '_wapbk_booking_status' );
                    $new_booking->update_status( $status );
                }
                
                // if automated sync is enabled, add the event back to the calendar
                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" ) {
                    $event_details = bkap_cancel_order::bkap_create_gcal_object( $order_id, $item_value, $order_obj );
                
                    // 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_key, $user_id, $post_id, false );
                
                    // add an order note, mentioning an event has been created for the item
                    $post_title = $event_details[ 'product_name' ];
                    $order_note = __( "Booking_details for $post_title have been exported to the Google Calendar", 'woocommerce-booking' );
                    $order_obj->add_order_note( $order_note );
                
                }
            }
        }
    }
    
    /**
     * This function creates an event details array
     * which contains all the details required to insert
     * an event in Google Calendar
     * 
     * @param string|int $order_id Order ID
     * @param array $item_details Order Item Details
     * @param WC_Order $order Order Object
     * @return array $event_details Event Details for GCal mapping
     * 
     * @since 3.5.2
     */

    public static function bkap_create_gcal_object( $order_id, $item_details, $order ) {
         
        $valid_date = false;
        if ( isset( $item_details[ 'wapbk_booking_date' ] ) ) {
            $valid_date = bkap_common::bkap_check_date_set( $item_details[ 'wapbk_booking_date' ] );
        }
    
        if ( $valid_date ) {
            $event_details = array();
    
            $event_details[ 'hidden_booking_date' ] = $item_details[ 'wapbk_booking_date' ];
    
            if ( isset( $item_details[ 'wapbk_checkout_date' ] ) && $item_details[ 'wapbk_checkout_date' ] != '' ) {
                $event_details[ 'hidden_checkout_date' ] = $item_details[ 'wapbk_checkout_date' ];
            }
    
            if ( isset( $item_details[ 'wapbk_time_slot' ] ) && $item_details[ 'wapbk_time_slot' ] != '' ) {
                $event_details[ 'time_slot' ] = $item_details[ 'wapbk_time_slot' ];
            }
    
            $event_details[ 'billing_email' ] = $order->get_billing_email();
            $event_details[ 'billing_first_name' ] = $order->get_billing_first_name();
            $event_details[ 'billing_last_name' ] = $order->get_billing_last_name();
            $event_details[ 'billing_address_1' ] = $order->get_billing_address_1();
            $event_details[ 'billing_address_2' ] = $order->get_billing_address_2();
            $event_details[ 'billing_city' ] = $order->get_billing_city();
    
            $event_details[ 'billing_phone' ] = $order->get_billing_phone();
            $event_details[ 'order_comments' ] = $order->get_customer_note();
            $event_details[ 'order_id' ] = $order_id;
    
            $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 = $order->get_shipping_last_name();
            if ( isset( $shipping_last_name ) && $shipping_last_name != '' ) {
                $event_details[ 'shipping_last_name' ] = $shipping_last_name;
            }
             
            $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 = $order->get_shipping_address_2();
            if ( isset( $shipping_address_2 ) && $shipping_address_2 != '' ) {
                $event_details[ 'shipping_address_2' ] = $shipping_address_2;
            }
             
            $shipping_city = $order->get_shipping_city();
            if ( isset( $shipping_city ) && $shipping_city != '' ) {
                $event_details[ 'shipping_city' ] = $shipping_city;
            }
             
            $_product = wc_get_product( $item_details[ 'product_id' ] );
    
            $post_title = $_product->get_title();
            $event_details[ 'product_name' ] = $post_title;
            $event_details[ 'product_qty' ] = $item_details[ 'qty' ];
    
            $event_details[ 'product_total' ] = $item_details[ 'line_total' ];
    
            return $event_details;
        }
    }

    /**
     * This function deletes booking for the products in order 
     * when the order is cancelled or refunded.
     * 
     * @hook woocommerce_order_status_cancelled
     * @hook woocommerce_order_status_refunded
     * @hook woocommerce_order_status_failed
     * 
     * @param string|int $order Order ID
     * 
     * @globals mixed $wpdb
     * @globals WP_Post $post - Order
     * 
     * @since 1.7.0
     */ 

    public static function bkap_woocommerce_cancel_order( $order_id ) {
        global $wpdb,$post;
        
        $array        =   array();
        $order_obj    =   new WC_order( $order_id );
        $order_items  =   $order_obj->get_items();
        $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 ) );
        $item_booking_id = 0;
        $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;
            $item_booking_id = $value->booking_id;
        }
        
        $i = 0;
        
        foreach( $order_items as $item_key => $item_value ) {

            if ( $item_value['_bkap_resch_rem_bal_order_id'] !== '' && $item_value['_bkap_resch_rem_bal_order_id'] !== null ){

                $related_order = wc_get_order( $item_value['_bkap_resch_rem_bal_order_id'] );

                if ( $order_obj->has_status( 'cancelled' ) ) {
                    $related_order->update_status( 'cancelled', 'Parent Order Cancelled.', false );
                }else if ( $order_obj->has_status( 'refunded' ) ) {
                    $related_order->update_status( 'refunded', 'Parent Order Refunded.', false );
                }else if ( $order_obj->has_status( 'failed' ) ) {
                    $related_order->update_status( 'failed', 'Parent Order Failed.', false );
                }
            }

            // check the booking status, if the status is cancelled, do not re-allot the item as that has already been done
            $_status = $item_value[ 'wapbk_booking_status' ];
            if ( ( isset( $_status ) && $_status != 'cancelled' ) || ! isset( $_status ) ) {
                $cancelled = false;
                // find the correct booking ID from the results array and pass the same
                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' ] ) {
                
                            if ( isset( $booking_data[ 'to_time' ] ) && '' != $booking_data[ 'to_time' ] ) {
                                $time = date( 'G:i', strtotime( $booking_data[ 'from_time' ] ) ) . ' - ' . date( 'G:i', strtotime( $booking_data[ 'to_time' ] ) );
                            } else {
                                $time = date( 'G:i', strtotime( $booking_data[ 'from_time' ] ) );
                            }
                            
                            if ( isset( $item_value[ 'wapbk_checkout_date' ] ) ) {
                                if ( $item_value[ 'wapbk_checkout_date' ] == $booking_data[ 'end_date' ] ) {
                                    $item_booking_id = $booking_id;
                                    break;
                                }
                            } else if( isset( $item_value[ 'wapbk_time_slot' ] ) ) {
                                if ( strpos( $item_value[ 'wapbk_time_slot' ] , ',' ) > 0 ) {
                                    $time_slot_list = explode( ',', $item_value[ 'wapbk_time_slot' ] );
                                    foreach( $time_slot_list as $t_key => $t_value ) {
                                        if ( $time == $t_value ) {
                                          $item_booking_id = $booking_id;
                                          self::bkap_reallot_item( $item_value, $item_booking_id, $order_id );
                                          $cancelled = true;
                                          // gcal sync for multiple time slots should be added here
                                        }
                                    }
                                    
                                } else if ( $item_value[ 'wapbk_time_slot'] == $time ) {
                                    $item_booking_id = $booking_id;
                                    break;
                                }
                            } else {
                                $item_booking_id = $booking_id;
                                break;
                            }
                        }
                    }
                }
                
                if ( ! $cancelled ) {
                    
                    // update the booking post status
                    $booking_post = bkap_common::get_booking_id( $item_key );
                    // update the booking post status
                    if ( $booking_post ) {
                        $new_booking = bkap_checkout::get_bkap_booking( $booking_post );
                        $new_booking->update_status( 'cancelled' );

                    }
                    
                    if ( isset ( $new_booking ) && isset( $new_booking->qty ) ) {
                        $bkap_qty = $new_booking->qty;
                    }
                    
                    if ( isset( $bkap_qty ) ) {
                        self::bkap_reallot_item( $item_value, $item_booking_id, $order_id, $bkap_qty );
                    } else {
                        self::bkap_reallot_item( $item_value, $item_booking_id, $order_id );
                    }
                    
                    $i++;
                    $product_id      =   bkap_common::bkap_get_product_id( $item_value['product_id'] );
                 
                    // check GCal sync
                    // user ID
                    $user = get_user_by( 'email', get_option( 'admin_email' ) );
                    $user_id = 0;
                    if ( isset( $user->ID ) ) {
                        $user_id = $user->ID;
                    } else {
                        // get the list of administrators
                        $args = array( 'role' => 'administrator', 'fields' => array( 'ID' ) );
                        $users = get_users( $args );
                        if ( isset( $users ) && count( $users ) > 0 ) {
                            $user_id = $users[ 0 ]->ID;
                        }
                    }
    
                    $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
                    
                    if ( isset( $booking_settings[ 'product_sync_integration_mode' ] ) && 'directly' == $booking_settings[ 'product_sync_integration_mode' ] ) {
                        $gcal_product_id = $product_id;
                    } else {
                        $gcal_product_id = 0;
                    }
                    
                    // check if tour operators are allowed to setup GCal
                    if ( 'yes' == get_option( 'bkap_allow_tour_operator_gcal_api' ) ) {
                        // if tour operator addon is active, pass the tour operator user Id else the admin ID
                        if ( function_exists( 'is_bkap_tours_active' ) && is_bkap_tours_active() ) {
                            if( isset( $booking_settings[ 'booking_tour_operator' ] ) &&  $booking_settings[ 'booking_tour_operator' ] != 0 ) {
                                $user_id = $booking_settings[ 'booking_tour_operator' ];
                            }
                        }
                    }
                    
                    // get the mode for the product settings as well if applicable
                    $gcal = new BKAP_Gcal();
                    if( $gcal->get_api_mode( $user_id, $gcal_product_id ) == "directly" ) {
                        $gcal->delete_event( $item_key, $user_id, $gcal_product_id );
                    }
                }
            }
        }
    }
    
    /**
     * Re-allots the booking date and/or time for each item in the order
     * 
     * @param array $item_value Order Item Values
     * @param int $booking_id Booking History Table ID
     * @param int $order_id Order ID
     * @param int $bkap_qty Quantity
     * 
     * @globals mixed $wpdb
     * @globals WP_Post $post
     * 
     * @since 1.7.0
     */

    public static function bkap_reallot_item( $item_value, $booking_id, $order_id, $bkap_qty = null ) {
        global $wpdb;
        global $post;
         
        $product_id      =   bkap_common::bkap_get_product_id( $item_value['product_id'] );
         
        $_product        =   wc_get_product( $product_id );
        $parent_id       = 0;
        /**
         * It will confirm that we have the product object. 
         * If it is boolean then we will not fetch the parent id.
         */
        if ( is_bool( $_product ) === false ) {
            $parent_id       = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $_product->get_parent() : bkap_common::bkap_get_parent_id( $product_id );
        }
    
        $details = array();
        
        $variation_id = '';
        if ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) >= 0 ) {
            $variation_id = $item_value->get_variation_id();
        } else if ( array_key_exists( "variation_id", $item_value ) ) {
            $variation_id = $item_value['variation_id'];
        }
    
        $booking_settings   =   get_post_meta( $product_id, 'woocommerce_booking_settings', true );
        
        if ( $bkap_qty != null && $bkap_qty >= 0 ) {
            $qty = $bkap_qty;
        } else if ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) >= 0 ) {
            $qty = $item_value->get_quantity();
        } else {
            $qty                =   $item_value['qty'];
        }
             
        if ( isset( $variation_id ) && $variation_id != 0 ) {
            // Product Attributes - Booking Settings
            $attribute_booking_data = get_post_meta( $product_id, '_bkap_attribute_settings', true );
        
            if ( is_array( $attribute_booking_data ) && count( $attribute_booking_data ) > 0 ) {
                $attr_qty = 0;
                
                if ( $bkap_qty != null && $bkap_qty >= 0 ) {
                    $attr_qty = $bkap_qty;
                } else {
                
                    foreach ( $attribute_booking_data as $attr_name => $attr_settings ) {
                     
                    // check if the setting is on
                        if ( isset( $attr_settings[ 'booking_lockout_as_value' ] ) && 'on' == $attr_settings[ 'booking_lockout_as_value' ] ) {
                            
                            if ( array_key_exists( $attr_name, $item_value ) && $item_value[ $attr_name ]  != 0 ) {
                            $attr_qty += $item_value[ $attr_name ];
                            }
                        }
                   }
                }
                if ( isset( $attr_qty ) && $attr_qty > 0 ){
                    $attr_qty = $attr_qty * $item_value['qty'];
                }
            }
        }
             
        if ( isset( $attr_qty ) && $attr_qty > 0 ){
            $qty = $attr_qty;
        }
         
        $from_time  =   '';
        $to_time    =   '';
        $date_date  =   '';
        $end_date   =   '';
        $start_date =   '';  
        if( isset( $booking_settings['booking_enable_multiple_day'] ) && $booking_settings['booking_enable_multiple_day'] == 'on' ) {
             
            if ( isset( $parent_id ) && $parent_id != '' ) {
                 
                // double the qty as we need to delete records for the child product as well as the parent product
                $qty               +=   $qty;
                $booking_id        +=   1;
                $first_record_id    =   $booking_id - $qty;
                $first_record_id   +=   1;
                $select_data_query  =   "DELETE FROM `".$wpdb->prefix."booking_history`
                                                WHERE ID BETWEEN %d AND %d";
                $results_data       =   $wpdb->query( $wpdb->prepare( $select_data_query, $first_record_id, $booking_id ) );
            }

            // if parent ID is not found, means its a normal product
            else {
                // DELETE the records using the ID in the booking history table.
                // The ID in the order history table, is the last record inserted for the order, so find the first ID by subtracting the qty
                $first_record_id    =   $booking_id - $qty;
                 
                $first_record_id   +=   1;
                 
                $select_data_query  =   "DELETE FROM `".$wpdb->prefix."booking_history`
                                            WHERE ID BETWEEN %d AND %d";
                $results_data       =   $wpdb->query( $wpdb->prepare( $select_data_query, $first_record_id, $booking_id ) );
                 
                 
            }

        } else if( isset( $booking_settings['booking_enable_time'] ) && $booking_settings['booking_enable_time'] == 'on' ) {
            $type_of_slot = apply_filters( 'bkap_slot_type', $product_id );

            if( $type_of_slot == 'multiple' ) {
                do_action( 'bkap_order_status_cancelled', $order_id, $item_value, $booking_id );
            }else {
                $select_data_query  =   "SELECT * FROM `".$wpdb->prefix."booking_history`
                                            WHERE id= %d";
                $results_data       =   $wpdb->get_results ( $wpdb->prepare( $select_data_query, $booking_id ) );
                $j                  =   0;
 
                foreach( $results_data as $k => $v ){
                    $start_date    =   $results_data[ $j ]->start_date;
                    $from_time     =   $results_data[ $j ]->from_time;
                    $to_time       =   $results_data[ $j ]->to_time;

                    if ( $from_time != '' && $to_time != '' || $from_time != '' ){
                        $parent_query = "";
                        if($to_time != ''){
                             
                             
                            //over lapaing time slots free booking product level
                            $query = "SELECT from_time, to_time, available_booking  FROM `".$wpdb->prefix."booking_history`
                                           WHERE post_id = '".$product_id."' AND
                                           start_date = '".$start_date."' AND
                                           status != 'inactive' ";
                            $get_all_time_slots = $wpdb->get_results( $query );

                            foreach( $get_all_time_slots as $time_slot_key => $time_slot_value){
                                 
                                $query_from_time_time_stamp = strtotime($from_time);
                                $query_to_time_time_stamp = strtotime($to_time);
                                $time_slot_value_from_time_stamp = strtotime($time_slot_value->from_time);
                                $time_slot_value_to_time_stamp = strtotime($time_slot_value->to_time);

                                $revised_available_booking = $time_slot_value->available_booking + $qty;
                                 
                                if( $query_to_time_time_stamp > $time_slot_value_from_time_stamp && $query_from_time_time_stamp < $time_slot_value_to_time_stamp ){

                                    if ( $time_slot_value_from_time_stamp != $query_from_time_time_stamp || $time_slot_value_to_time_stamp != $query_to_time_time_stamp ) {
                                        $query = "UPDATE `".$wpdb->prefix."booking_history`
                                                    SET available_booking = ".$revised_available_booking."
                                                    WHERE post_id = '".$product_id."' AND
                                                    start_date = '".$start_date."' AND
                                                    from_time = '".$time_slot_value->from_time."' AND
                                                    to_time = '".$time_slot_value->to_time."' AND
                                                    status != 'inactive' AND
                                                    total_booking > 0";

                                        $wpdb->query( $query );
                                    }
                                }
                            }
                            $query = "UPDATE `".$wpdb->prefix."booking_history`
                                            SET available_booking = available_booking + ".$qty."
                                            WHERE
                                            id = '".$booking_id."' AND
                                            start_date = '".$start_date."' AND
                                            from_time = '".$from_time."' AND
                                            to_time = '".$to_time."' AND
                                            status != 'inactive' AND
                                            total_booking > 0";
                            //Update records for parent products - Grouped Products
                            if ( isset( $parent_id ) && $parent_id != '' ) {
                                $parent_query   =   "UPDATE `".$wpdb->prefix."booking_history`
                                                         SET available_booking = available_booking + ".$qty."
                                                         WHERE
                                                         post_id = '".$parent_id."' AND
                                                         start_date = '".$start_date."' AND
                                                         from_time = '".$from_time."' AND
                                                         to_time = '".$to_time."' AND
                                                         status != 'inactive' AND 
                                                         total_booking > 0";
                                
                                $wpdb->query( $parent_query );
                                
                                $select         =   "SELECT * FROM `".$wpdb->prefix."booking_history`
                                                        WHERE post_id = %d AND
                                                        start_date = %s AND
                                                        from_time = %s AND
                                                        to_time = %s AND
                                                        status != 'inactive'";
                                                        
                                $select_results =   $wpdb->get_results( $wpdb->prepare( $select, $parent_id, $start_date, $from_time, $to_time ) );
                                 
                                foreach( $select_results as $k => $v ) {
                                    $details[ $product_id ] = $v;
                                }
                            }

                            $select          =   "SELECT * FROM `".$wpdb->prefix."booking_history`
                                                     WHERE post_id = %d AND
                                                     start_date = %s AND
                                                     from_time = %s AND
                                                     to_time = %s AND
                                                     status != 'inactive' ";
                            $select_results  =   $wpdb->get_results( $wpdb->prepare( $select, $product_id, $start_date, $from_time, $to_time ) );

                            foreach( $select_results as $k => $v ) {
                                $details[ $product_id ] = $v;
                            }

                        } else {
                            $query   =   "UPDATE `".$wpdb->prefix."booking_history`
                                              SET available_booking = available_booking + ".$qty."
                                              WHERE
                                              id = '".$booking_id."' AND
                                              start_date = '".$start_date."' AND
                                              from_time = '".$from_time."' AND
                                              status != 'inactive' AND 
                                              total_booking > 0";
                             
                            //Update records for parent products - Grouped Products
                            if ( isset( $parent_id ) && $parent_id != '' ) {
                                $parent_query   =   "UPDATE `".$wpdb->prefix."booking_history`
                                                        SET available_booking = available_booking + ".$qty."
                                                        WHERE
                                                        post_id = '".$parent_id."' AND
                                                        start_date = '".$start_date."' AND
                                                        from_time = '".$from_time."' AND
                                                        status != 'inactive' AND 
                                                        total_booking > 0";
                                
                                $wpdb->query( $parent_query );
                                
                                $select         =   "SELECT * FROM `".$wpdb->prefix."booking_history`
                                                        WHERE post_id = %d AND
                                                        start_date = %s AND
                                                        from_time = %s AND
                                                        status != 'inactive' ";
                                $select_results =   $wpdb->get_results( $wpdb->prepare( $select, $parent_id, $start_date, $from_time ) );
                                 
                                foreach( $select_results as $k => $v ) {
                                    $details[$product_id] = $v;
                                }
                            }

                            $select          =   "SELECT * FROM `".$wpdb->prefix."booking_history`
                                                     WHERE post_id = %d AND
                                                     start_date = %s AND
                                                     from_time = %s AND 
                                                     status != 'inactive' ";
                            $select_results  =   $wpdb->get_results( $wpdb->prepare($select,$product_id,$start_date,$from_time) );

                            foreach( $select_results as $k => $v ) {
                                $details[ $product_id ] = $v;
                            }
                        }
                        // Run the Update query for the product
                        $wpdb->query( $query );
                    }
                    $j++;
                }
                self::reallot_global_timeslot( $start_date, $from_time, $to_time, $booking_settings, $details, $qty );
            }
        } else {
            $select_data_query   =   "SELECT * FROM `".$wpdb->prefix."booking_history`
                                         WHERE id= %d";
            $results_data        =   $wpdb->get_results ( $wpdb->prepare( $select_data_query, $booking_id ) );
            $j                   =   0;

            foreach( $results_data as $k => $v ) {
                $start_date     =   $results_data[$j]->start_date;
                $from_time      =   $results_data[$j]->from_time;
                $to_time        =   $results_data[$j]->to_time;
                $query          =   "UPDATE `".$wpdb->prefix."booking_history`
                                        SET available_booking = available_booking + ".$qty."
                                        WHERE
                                        id = '".$booking_id."' AND
                                        start_date = '".$start_date."' AND
                                        from_time = '' AND
                                        to_time = '' AND
                                        status != 'inactive' AND 
                                        total_booking > 0";
                $wpdb->query( $query );
                 
                //Update records for parent products - Grouped Products
                if ( isset( $parent_id ) && $parent_id != '' ) {
                    $parent_query  =   "UPDATE `".$wpdb->prefix."booking_history`
                                            SET available_booking = available_booking + ".$qty."
                                            WHERE
                                            post_id = '".$parent_id."' AND
                                            start_date = '".$start_date."' AND
                                            from_time = '' AND
                                            to_time = '' AND
                                            status != 'inactive' AND 
                                            total_booking > 0";
                    $wpdb->query( $parent_query );
                }
            }
            $j++;
        }
     
    }

    /**
     * Reallot the global timeslot when a booking is trashed
     * 
     * @param string $start_date Start Date
     * @param string $from_time From Time
     * @param string $to_time To Time
     * @param mixed $booking_settings Booking Settings
     * @param mixed $details Booking Details
     * @param int $qty Quantity
     * 
     * @globals mixed $wpdb
     * 
     * @since 2.3.5
     */

    public static function reallot_global_timeslot( $start_date, $from_time, $to_time, $booking_settings, $details, $qty ) {
        global $wpdb;
        
        $book_global_settings    =   json_decode( get_option( 'woocommerce_booking_global_settings' ) );
        $global_timeslot_lockout =   '';
        $label                   =   get_option( "book_item-meta-date" );
        $hidden_date             =   '';
    
        if ( isset( $start_date ) && $start_date != '' ) {
            $hidden_date = date( 'd-n-Y', strtotime( $start_date ) );
        }
    
        if ( isset( $booking_settings['booking_time_settings'][ $hidden_date ] ) ){
            $lockout_settings = $booking_settings['booking_time_settings'][ $hidden_date ];
        } else {
            $lockout_settings = array();
        }
         
        if(count($lockout_settings) == 0){
            $week_day = date('l',strtotime($hidden_date));
            $weekdays = bkap_get_book_arrays('bkap_weekdays');
            $weekday = array_search($week_day,$weekdays);
            if (isset($booking_settings['booking_time_settings'][$weekday])){
                $lockout_settings = $booking_settings['booking_time_settings'][$weekday];
            }
            else {
                $lockout_settings = array();
            }
        }
         
        if(count($lockout_settings) > 0) {
            $week_day = date('l',strtotime($hidden_date));
            $weekdays = bkap_get_book_arrays('bkap_weekdays');
            $weekday = array_search($week_day,$weekdays);
            if (isset($booking_settings['booking_time_settings'][$weekday])){
                $lockout_settings = $booking_settings['booking_time_settings'][$weekday];
            } else {
                $lockout_settings = array();
            }
             
        }
    
        $from_lockout_time = explode(":",$from_time);
        if( isset( $from_lockout_time[0] ) ){
            $from_hours = $from_lockout_time[0];
        } else {
            $from_hours = '';
        }
         
        if( isset( $from_lockout_time[1] ) ){
            $from_minute = $from_lockout_time[1];
        } else {
            $from_minute = '';
        }
         
        if( $to_time != '' ) {
            $to_lockout_time    =   explode( ":", $to_time );
            $to_hours           =   $to_lockout_time[0];
            $to_minute          =   $to_lockout_time[1];
        } else {
            $to_hours           =   '';
            $to_minute          =   '';
        }
    
        if( count( $lockout_settings ) > 0 ) {
             
            foreach( $lockout_settings as $l_key => $l_value ) {
    
                if( $l_value['from_slot_hrs'] == $from_hours && $l_value['from_slot_min'] == $from_minute && $l_value['to_slot_hrs'] == $to_hours && $l_value['to_slot_min'] == $to_minute ) {
                     
                    if ( isset($l_value['global_time_check'] ) ){
                        $global_timeslot_lockout = $l_value['global_time_check'];
                    }else{
                        $global_timeslot_lockout = '';
                    }
                     
                }
            }
        }
    
        if(isset($book_global_settings->booking_global_timeslot) && $book_global_settings->booking_global_timeslot == 'on' || isset($global_timeslot_lockout) && $global_timeslot_lockout == 'on') {
             
            $args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
            $product = query_posts( $args );
            foreach($product as $k => $v) {
                $product_ids[] = $v->ID;
            }
             
            foreach( $product_ids as $k => $v ) {
    
                $duplicate_of      =   bkap_common::bkap_get_product_id( $v );
    
                $booking_settings  =   get_post_meta( $duplicate_of, 'woocommerce_booking_settings', true );
    
                if ( isset( $booking_settings['booking_enable_time'] ) && $booking_settings['booking_enable_time'] == 'on' ) {
                     
                    if ( isset( $details ) && count( $details ) > 0 ) {
    
                        if ( !array_key_exists( $duplicate_of, $details ) ) {
                             
                            foreach( $details as $key => $val ) {
                                $start_date    =   $val->start_date;
                                $from_time     =   $val->from_time;
                                $to_time       =   $val->to_time;
                                $revised_available_booking = '';
                                if($to_time != "") {
    
                                    //over lapaing time slots free booking product level
                                    $query = "SELECT from_time, to_time, available_booking  FROM `".$wpdb->prefix."booking_history`
                                                       WHERE post_id = '".$duplicate_of."' AND
                                                       start_date = '".$start_date."' AND
                                                       status !=  'inactive' ";
                                    $get_all_time_slots = $wpdb->get_results( $query );
                                     
                                    foreach( $get_all_time_slots as $time_slot_key => $time_slot_value){
    
                                        $query_from_time_time_stamp = strtotime($from_time);
                                        $query_to_time_time_stamp = strtotime($to_time);
                                        $time_slot_value_from_time_stamp = strtotime($time_slot_value->from_time);
                                        $time_slot_value_to_time_stamp = strtotime($time_slot_value->to_time);
    
                                        if( $query_to_time_time_stamp > $time_slot_value_from_time_stamp && $query_from_time_time_stamp < $time_slot_value_to_time_stamp ){
                                             
                                            if ( $time_slot_value_from_time_stamp != $query_from_time_time_stamp || $time_slot_value_to_time_stamp != $query_to_time_time_stamp ) {
                                                $query = "UPDATE `".$wpdb->prefix."booking_history`
                                                                SET available_booking = available_booking + ".$qty."
                                                                WHERE post_id = '".$duplicate_of."' AND
                                                                start_date = '".$start_date."' AND
                                                                from_time = '".$time_slot_value->from_time."' AND
                                                                to_time = '".$time_slot_value->to_time."' AND
                                                                status != 'inactive' AND
                                                                total_booking > 0";
                                                 
                                                $wpdb->query( $query );
                                            }
                                        }
                                    }
    
                                    $query = "UPDATE `".$wpdb->prefix."booking_history`
                                                SET available_booking = available_booking + ".$qty."
                                                WHERE post_id = '".$duplicate_of."' AND
                                                start_date = '".$start_date."' AND
                                                from_time = '".$from_time."' AND
                                                to_time = '".$to_time."' AND
                                                status != 'inactive' AND 
                                                total_booking > 0";
                                    $wpdb->query($query);
                                } else {
                                    $query    =   "UPDATE `".$wpdb->prefix."booking_history`
                                                      SET available_booking = available_booking + ".$qty."
                                                      WHERE post_id = '".$duplicate_of."' AND
                                                      start_date = '".$start_date."' AND
                                                      from_time = '".$from_time."' AND
                                                      status != 'inactive' AND 
                                                      total_booking > 0";
                                    $wpdb->query( $query );
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * This will reallocate the bookings when order status changed from failed to processing,completed and on-hold.
     *
     * @hook woocommerce_order_status_failed_to_processing
     * @hook woocommerce_order_status_failed_to_completed
     * @hook woocommerce_order_status_failed_to_on-hold
     * 
     * @param array $args Contains Order Params.
     * 
     * @globals mixed $woocommerce
     * @since 4.2.0
     */
        
    public static function bkap_reallocate_booking_when_order_status_failed_to_processing( $args = array() ) {
         
        global $woocommerce;
         
        $order   = wc_get_order( $args );
        $details = array();
         
        foreach ( $order->get_items() as $order_item_id => $item ) {
    
            $booking          = array();
            $product_bookable = "";
            $parent_id        = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $_product->get_parent() : bkap_common::bkap_get_parent_id( $item[ 'product_id' ] );
            $post_id          = bkap_common::bkap_get_product_id( $item['product_id'] );
            $quantity         = $item['qty'];
            $product_bookable = bkap_common::bkap_get_bookable_status( $post_id );
    
            if( $product_bookable ){
    
                $booking = array( 'date'                   => $item['_wapbk_booking_date'],
                    'hidden_date'            => date("d-m-Y", strtotime($item['_wapbk_booking_date'] ) ),
                    'date_checkout'          => $item['wapbk_checkout_date'],
                    'hidden_date_checkout'   => date("d-m-Y", strtotime($item['_wapbk_checkout_date'] ) ),
                    'price'                  => $item['cost'],
                    'time_slot'              => $item['_wapbk_time_slot']
                );
                 
                $details = bkap_checkout::bkap_update_lockout( $order_id, $post_id, $parent_id, $quantity, $booking );
                // update the global time slot lockout
                if( isset( $booking['time_slot'] ) && $booking['time_slot'] != "" ) {
                    bkap_checkout::bkap_update_global_lockout( $post_id, $quantity, $details, $booking);
                }
            }
    
        }
    }
}
?>
API documentation generated by ApiGen