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 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 
<?php 
/**
 * Bookings and Appointment Plugin for WooCommerce
 *
 * Class for handling Fixed Blocks and Price Ranges
 *
 * @author   Tyche Softwares
 * @package  BKAP/Block-Pricing
 * @category Classes
 */
if( session_id() === '' ){
    //session has not started
    session_start();
}

if ( !class_exists( 'bkap_block_booking' ) ) {

    /**
     * Class for handling Fixed Blocks and Price Ranges
     * @class bkap_block_booking class
     **/
    class bkap_block_booking {

        /**
         * Default constructor
         *
         * @since 4.1.0
         */

        public function __construct() {
            // Initialize settings
            //register_activation_hook( __FILE__, array( &$this, 'bkap_block_booking_activate' ) );
            
            $this->variable_block_price = 0;
            // 
            add_action( 'admin_init',                               array( &$this, 'bkap_load_ajax_block_pricing' ) );
            
            // Adding Block Pricing tab in Booking Meta Box.
            add_action( 'bkap_add_tabs',                            array( &$this, 'block_pricing_tab' ), 5, 1 );
            
            // used to add new settings on the product page booking box
            add_action( 'bkap_after_listing_enabled',               array( &$this, 'bkap_block_pricing_show_field_settings' ), 5, 1 );
            
            // Fixed Block.
            add_action( 'bkap_display_multiple_day_updated_price',  array( &$this, 'bkap_fixed_block_show_updated_price'), 6, 7 );
            // Price by Range.
            add_action( 'bkap_display_multiple_day_updated_price',  array( &$this, 'bkap_price_range_show_updated_price' ), 5, 7 );
            
            //add_action( 'woocommerce_before_add_to_cart_form',      array( &$this, 'bkap_fixed_block_before_add_to_cart' ) );
            
            add_action( 'bkap_before_booking_form',                 array( &$this, 'bkap_fixed_block_booking_after_add_to_cart' ), 7, 1 );
            
            add_action( 'bkap_display_price_div',                   array( &$this, 'bkap_fixed_block_display_price'), 10, 1 );
            
            // Copy the exisiting variable blocks to the new product when the product is duplicated
            add_action( 'bkap_product_addon_duplicate',             array( &$this, 'price_range_product_duplicate' ), 10, 2 );
            
            add_action( 'bkap_before_booking_form',                 array( &$this, 'bkap_price_range_booking_after_add_to_cart' ), 10, 1 );
            
        }
        
        /**
         * This function is used to load ajax functions required for
         * Fixed Blocks & Price Ranges
         * 
         * @since 4.1.0
         */

        function bkap_load_ajax_block_pricing() {
            
            // ajax for deleting a single fixed block.
            add_action( 'wp_ajax_bkap_delete_block',               array( &$this, 'bkap_delete_block' ) );
            
            // ajax for deleting all fixed blocks.
            add_action( 'wp_ajax_bkap_delete_all_blocks',          array( &$this, 'bkap_delete_all_blocks' ) );
            
            // ajax for clearing the block pricing options.
            add_action( 'wp_ajax_bkap_block_pricing_options',      array( &$this, 'bkap_block_pricing_options' ) );
            
            // ajax for deleting a single price range.
            add_action( 'wp_ajax_bkap_delete_range',               array( &$this, 'bkap_delete_range' ) );
            
            // ajax for deleting all price ranges.
            add_action( 'wp_ajax_bkap_delete_all_ranges',          array( &$this, 'bkap_delete_all_ranges' ) );
        }
        
        /**
         * This function is used to delete existing Fixed blocks from the 
         * table in the Booking meta box
         * 
         * @since 4.1.0
         */

        function bkap_delete_block(){
            
            $product_id = $_POST['post_id'];
            $key        = $_POST['fixed_block_key'];
            
            $bkap_fixed_blocks_data = get_post_meta( $product_id, "_bkap_fixed_blocks_data", true );
            
            if( $bkap_fixed_blocks_data != "" ){
                
                if ( array_key_exists( $key, $bkap_fixed_blocks_data ) ) {
                    unset( $bkap_fixed_blocks_data[ $key ] );
                }
            }
            update_post_meta( $product_id, "_bkap_fixed_blocks_data", $bkap_fixed_blocks_data );
            
            die();
        }
        
        /**
         * This function is used to delete existing Price Ranges from the 
         * table in the Booking meta box.
         * 
         * @since 4.1.0
         */

        function bkap_delete_range(){
             
            $product_id = $_POST['post_id'];
            $key        = $_POST['price_range_key'];
            
            $bkap_price_range_data = get_post_meta( $product_id, "_bkap_price_range_data", true );
             
            if( $bkap_price_range_data != "" ){
                 
                if ( array_key_exists( $key, $bkap_price_range_data ) ) {
                    unset( $bkap_price_range_data[ $key ] );
                }
            }
            update_post_meta( $product_id, "_bkap_price_range_data", $bkap_price_range_data );
             
            die();
        }
        
        /**
         * This function is used to delete all the Fixed Blocks present
         * for a product. 
         * 
         * @since 4.1.0
         */

         function bkap_delete_all_blocks(){
        
            $post_id    = $_POST[ 'post_id' ];
            $blank_data = "";
            update_post_meta( $post_id, '_bkap_fixed_blocks_data', $blank_data );
                
            die();
        }
        
        /**
         * This function are used to delete all the Price Ranges present
         * for a product.
         * 
         * @since 4.1.0
         */

        function bkap_delete_all_ranges(){
                
            $post_id    = $_POST[ 'post_id' ];
            $blank_data = "";
            update_post_meta( $post_id, '_bkap_price_range_data', $blank_data );
        
            die();
        }
        
        /**
         * This function are used to clear block pricing option radio button selection
         * in the Booking meta box.
         * 
         * @since 4.1.0
         */

        function bkap_block_pricing_options(){
                
            $post_id    = $_POST[ 'product_id' ];
            $blank_data = "";
            
            update_post_meta( $post_id, '_bkap_price_ranges', $blank_data );
            update_post_meta( $post_id, '_bkap_fixed_blocks', $blank_data );
            
            die();
        }
        
        /**
         * This function will add the Block Pricing tab in the Booking meta box.
         * 
         * @hook bkap_add_tabs
         * @since 4.1.0
         */

        function block_pricing_tab( $product_id ) {
        ?>
            <li>
             <a id="block_booking" class="bkap_tab" style="display:none">
                 <i class="fa fa-align-justify" aria-hidden="true"></i><?php _e( 'Block Pricing', 'woocommerce-booking' ); ?>
             </a>
            </li>
        <?php 
        }
            
        /**
         * This function add the content for the Booking meta box->Block Pricing tab.
         *
         * @param integer $product_id Product ID
         * @global object $post WP_Post
         * 
         * @hook bkap_after_listing_enabled
         * @since 4.1.0
         */

        public function bkap_block_pricing_show_field_settings( $product_id ) {
            global $post;
        
            $post_id = 0;
            if ( isset( $post->ID ) ) {
                $post_id = $post->ID;
            }
                
            $duplicate_of       = bkap_common::bkap_get_product_id( $post_id );
            $booking_settings   = get_post_meta( $duplicate_of, 'woocommerce_booking_settings', true );
            ?>
            <div id="block_booking_page">
                <?php
                   // This is the section where one can enable the block pricing option.
                   $this->bkap_enable_price_by_range_or_fixed_block_booking( $duplicate_of );
                ?>
                <hr/>               
                <?php
                   // This functions are for Fixed Block Booking and Price By Range tables.  
                   $this->bkap_fixed_block_booking_table( $duplicate_of, $booking_settings );
                   $this->bkap_price_range_booking_table( $duplicate_of, $booking_settings );
                ?>
                <div id='block_pricing_update_notification' style='display:none;'></div>
            </div>
        <?php
        }
            
        /**
         * Create and return an array of Fixed Blocks data.
         * Executed when settings are saved using either from the Booking meta box
         * or the product is saved/updated.
         *
         * @param $product_id Product ID
         * @param object $clean_fixed_block_data
         * @return array $array_of_all_fixed_block_data
         * @since 4.1.0
         */

         static function bkap_updating_fixed_block_data_in_db( $product_id, $clean_fixed_block_data ) {
            
            $fixed_block_data        = $clean_fixed_block_data->bkap_fixed_block_data;
            $fixed_block_each_data   = explode( ';', $fixed_block_data );
            
            array_pop($fixed_block_each_data);
            
            $array_of_all_fixed_block_data = array();
            
            
            foreach( $fixed_block_each_data as $fixed_block_each_data_value ){
                $array_of_individual_fixed_block_data                    = explode( '&&', $fixed_block_each_data_value );
                $array_of_all_fixed_block_data[$array_of_individual_fixed_block_data[5]]['block_name']         = $array_of_individual_fixed_block_data[0];
                $array_of_all_fixed_block_data[$array_of_individual_fixed_block_data[5]]['number_of_days']     = $array_of_individual_fixed_block_data[1];
                $array_of_all_fixed_block_data[$array_of_individual_fixed_block_data[5]]['start_day']          = $array_of_individual_fixed_block_data[2];
                $array_of_all_fixed_block_data[$array_of_individual_fixed_block_data[5]]['end_day']            = $array_of_individual_fixed_block_data[3];
                $array_of_all_fixed_block_data[$array_of_individual_fixed_block_data[5]]['price']              = $array_of_individual_fixed_block_data[4];
                
            }
            
            return $array_of_all_fixed_block_data;
        }

        /**
         * Create and return an array of Price Ranges data.
         * Executed when settings are saved using either from the Booking meta box
         * or the product is saved/updated.
         *
         * @param $product_id Product ID
         * @param object $clean_price_range_data
         * @return array $array_of_all_price_range_data
         * @since 4.1.0
         */

        static function bkap_updating_price_range_data_in_db( $product_id, $clean_price_range_data ) {
            
            $price_range_data        = $clean_price_range_data->bkap_price_range_data;
            $price_range_each_data   = explode( ';;', $price_range_data );
             
            array_pop( $price_range_each_data );
             
            $array_of_all_price_range_data = array();
            
            $product_attributes       = get_post_meta( $product_id, '_product_attributes', true );

            $product_attributes_keys  = array();
            if( is_array( $product_attributes ) && $product_attributes > 0 ){
               $product_attributes_keys  = array_keys( $product_attributes );
            }
            
            foreach( $price_range_each_data as $price_range_each_data_value ){
                $array_of_individual_price_range_data = explode( '~~', $price_range_each_data_value );
                
                $key_of_array                         = end( $array_of_individual_price_range_data );
                $count                                = count( $array_of_individual_price_range_data );      
                
                if( $count > 5 ){
                    $count_new = $count - 6;
                    for( $i = 0; $i <= $count_new; $i++ ){
                        $attribute = $product_attributes_keys[ $i ];
                        $array_of_all_price_range_data[$key_of_array][ $attribute ]= $array_of_individual_price_range_data[ $i ];
                    }
                }
                
                $min_number  = ( isset( $array_of_individual_price_range_data[ $count-5 ] ) && !empty( $array_of_individual_price_range_data[ $count-5 ] ) ) ? $array_of_individual_price_range_data[ $count-5 ] : "";
                $max_number  = ( isset( $array_of_individual_price_range_data[ $count-4 ] ) && !empty( $array_of_individual_price_range_data[ $count-4 ] ) ) ? $array_of_individual_price_range_data[ $count-4 ] : "";
                $p_d_price   = ( isset( $array_of_individual_price_range_data[ $count-3 ] ) && !empty( $array_of_individual_price_range_data[ $count-3 ] ) ) ? $array_of_individual_price_range_data[ $count-3 ] : 0;
                $f_price     = ( isset( $array_of_individual_price_range_data[ $count-2 ] ) && !empty( $array_of_individual_price_range_data[ $count-2 ] ) ) ? $array_of_individual_price_range_data[ $count-2 ] : 0;
                
                $array_of_all_price_range_data[$key_of_array]['min_number']              = $min_number;
                $array_of_all_price_range_data[$key_of_array]['max_number']              = $max_number;
                $array_of_all_price_range_data[$key_of_array]['per_day_price']           = $p_d_price;
                $array_of_all_price_range_data[$key_of_array]['fixed_price']             = $f_price;
                
                 
            }
            return $array_of_all_price_range_data;
        }
            
        /**
         * This function add the fixed block dropdown on the frontend product page
         * as per the settings selected when Enable Fixed Block Booking is enabled.
         * 
         * @param $product_id - Product ID
         * 
         * @hook bkap_before_booking_form
         * @since 4.1.0
         */

        function bkap_fixed_block_booking_after_add_to_cart( $product_id ){
            
            $curr_lang = "en";
            
            // Fetching the current language of WPML or any other transaltion plugin is active.
            if ( defined('ICL_LANGUAGE_CODE') ){
                 
                if( ICL_LANGUAGE_CODE == 'en' ) {
                    $curr_lang = "en";
                } else{
                    $curr_lang = ICL_LANGUAGE_CODE;
                }
            }               
            
            //$duplicate_of     =   bkap_common::bkap_get_product_id( $post->ID );
            $duplicate_of     =   $product_id;
            $booking_settings =   get_post_meta( $duplicate_of, 'woocommerce_booking_settings', true );

            if ( ( isset( $booking_settings['booking_enable_multiple_day'] ) && $booking_settings['booking_enable_multiple_day'] == 'on' )
               && ( isset( $booking_settings['booking_fixed_block_enable'] ) && $booking_settings['booking_fixed_block_enable'] == 'booking_fixed_block_enable' ) ) {
                //print( '<div id="bkap-booking-form" class="bkap-booking-form">' );
                
                // Getting Fixed block data from the post meta.
                $results = $this->bkap_get_fixed_blocks( $duplicate_of );
                
                if ( isset( WC()->cart ) ) {

                    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                                    
                           if( array_key_exists( 'bkap_booking', $values ) ) {
                                    $booking       = $values['bkap_booking'];
                                    $hidden_date   = $booking[0]['hidden_date'];
                                    
                                    if( array_key_exists( "hidden_date_checkout", $booking[0] ) ) {
                                        $hidden_date_checkout = $booking[0]['hidden_date_checkout'];
                                    }
                            }
                                break;
                    }
                }
                
                // Getting first key of $result array
                reset($results);
                $first_key = key($results);
                                    
                if ( count( $results ) > 0) {

                    printf ('<label>'. __( get_option( "book_fixed-block-label" ), "woocommerce-booking" ) . ': </label>
                             <br/>
                             <select name="block_option" id="block_option" >' );
                    
                    foreach ( $results as $key => $value ) {
                        
                        $name_msg     = 'bkap_fixed_' . $key . '_block_name';
                        $block_name   = $value['block_name'];
                        $block_name   = $this->bkap_get_translated_texts( $name_msg, $block_name, $curr_lang );
                        
                        printf('<option value=%s&%s&%s>%s</option>', $value['start_day'], $value['number_of_days'], $value['price'], $block_name );
                    } 
                    printf ('</select> <br/> <br/>');
                
                if ( count( $results ) >= 0 ) {
                    $sd=$results[$first_key]['start_day'];
                    $nd=$results[$first_key]['number_of_days'];
                    $pd=$results[$first_key]['price'];
                }
                echo ' <input type="hidden" id="block_option_enabled"  name="block_option_enabled" value="on"/> <input type="hidden" id="block_option_start_day"  name="block_option_start_day" value="'.$sd.'"/> <input type="hidden" id="block_option_number_of_day"  name="block_option_number_of_day" value="'.$nd.'"/><input type="hidden" id="block_option_price"  name="block_option_price" value="'.$pd.'"/>';  
                } else  {
                    $number_of_fixed_price_blocks = 0;
                    echo ' <input type="hidden" id="block_option_enabled"  name="block_option_enabled" value="off"/> <input type="hidden" id="block_option_start_day"  name="block_option_start_day" value=""/> <input type="hidden" id="block_option_number_of_day"  name="block_option_number_of_day" value=""/><input type="hidden" id="block_option_price"  name="block_option_price" value=""/>';
                }
              }
        }

        /**
        * This function adds the price div to display the Booking Price
        * on the Product page.
        * 
        * @param integer $product_id - Product ID
        * 
        * @hook bkap_display_price_div
        * @since 4.1.0
        */

        function bkap_fixed_block_display_price( $product_id ) {
            
            $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
            
            if ( isset( $_POST['booking_fixed_block_enable'] ) && $_POST['booking_partial_payment_radio'] != '' ):
                $currency_symbol   = get_woocommerce_currency_symbol();
                if( has_filter( 'bkap_show_addon_price' ) ) {
                    $show_price = apply_filters( 'bkap_show_addon_price', '' );
                } else {
                    $show_price = 'show';
                }
                print('<div id="show_addon_price" name="show_addon_price" class="show_addon_price" style="display:'.$show_price.';">'.$currency_symbol.' 0</div>');
            endif;
        }
        
        /**
         * This function is to display Block Pricing radio button settings
         * in Booking meta box->Block Pricing tab.
         * 
         * @param integer $duplicate_of - Product ID
         * @since 4.1.0 
         **/

        function bkap_enable_price_by_range_or_fixed_block_booking( $duplicate_of ){
            
            $bkap_fixed_blocks_check = "";
            $bkap_price_ranges_check = "";
            
            $bkap_fixed_blocks =   get_post_meta( $duplicate_of, '_bkap_fixed_blocks', true );
            $bkap_price_ranges =   get_post_meta( $duplicate_of, '_bkap_price_ranges', true );
            
            if( isset( $bkap_fixed_blocks ) && $bkap_fixed_blocks != "" )
                $bkap_fixed_blocks_check = "checked";
                
            if( isset( $bkap_price_ranges ) && $bkap_price_ranges != "" )
                $bkap_price_ranges_check = "checked";
            
            ?>
            <div id="enable_block_pricing_section" class="block_pricing_flex_main">
                    
                <div class="block_pricing_flex_child pricing_left">
                    <label><?php _e( 'Block Pricing', 'woocommerce-booking' );?></label>
                </div>
                
                <div class="block_pricing_flex_child pricing_center block_type_main"> 
                    <div class="block_pricing_flex_child_block_type" >
                        <input type="radio" id="booking_fixed_block_enable" name="bkap_enable_block_pricing_type" value="booking_fixed_block_enable" onclick="bkap_save_fixed_block_settings()" <?php echo $bkap_fixed_blocks_check; ?>></input>
                        <label for="booking_fixed_block_enable"> <?php _e( 'Fixed Block Booking', 'woocommerce-booking' );?> </label>
                    </div>
                    
                    <div class="block_pricing_flex_child_block_type" >
                       <input type="radio" id="booking_block_price_enable" name="bkap_enable_block_pricing_type" value="booking_block_price_enable" onclick="bkap_save_price_by_range_settings()" <?php echo $bkap_price_ranges_check; ?>></input>
                       <label for="booking_block_price_enable"> <?php _e( 'Price By Range Of Nights', 'woocommerce-booking' );?> </label>
                    </div>
                </div>
                
                <div class="block_pricing_flex_child pricing_right bkap_help">
                    <a href="#" class="bkap_clear_block_pricing_selection" style="font-size: 12px;font-style: italic;" >Clear selection</a>
                    
                    <img class="help_tip" width="16" height="16"  data-tip="<?php _e( 'Select Fixed Block Booking option if you want customers to book or rent this product for fixed number of days. </br> Select Price By Range Of Nights option if you want to charge customers different prices for different day ranges.', 'woocommerce-booking' );?>" src="<?php echo plugins_url() ;?>/woocommerce/assets/images/help.png" />
                </div>
                     
              </div>
            
            <?php 
        }
        
        /**
         * Price By Range Of Days table in Booking meta box->Block Pricing.
         *
         * @param $product_id - Product ID
         * @param array $booking_settings - Booking Settings for the product.
         * @since 4.1.0
         **/

        public static function bkap_price_range_booking_table( $product_id, $booking_settings ){
            
            $bkap_price_range_option     = get_post_meta( $product_id, '_bkap_price_ranges', true );
            $bkap_fixed_price_option     = get_post_meta( $product_id, '_bkap_fixed_blocks', true );
            $display_price_range_table   = "display:none";
            
            if( $bkap_price_range_option == "" && $bkap_fixed_price_option == "" ){
                $display_price_range_table = "";
            }
            
            $disable_block_pricing_class = "bkap_disable_block_pricing";
             
            if( isset( $bkap_price_range_option ) && $bkap_price_range_option == "booking_block_price_enable" ){
                $disable_block_pricing_class = "";
                $display_price_range_table   = "";
            }
            
            $attribute_count       = 0;
            $product_attributes    = get_post_meta( $product_id, '_product_attributes', true );
            
            if( is_array( $product_attributes ) && $product_attributes > 0 ){
               $attribute_count = count( $product_attributes ); 
               $attribute_count += 1;
            }else{
               $attribute_count  = 2;
            }
            ?>
            
            <!-- Table for Price by range of days -->
            <div class="bkap_price_range_booking <?php echo $disable_block_pricing_class; ?>" style="<?php echo $display_price_range_table; ?>">
                 
                <div>
                   <h4><?php _e( 'Price by range of nights :', 'woocommerce-booking' ); ?></h4>
                </div>
                
                <table id="bkap_price_range_booking_table" >
                
                <?php
                 
                 self::bkap_get_price_range_booking_heading( $product_id, $booking_settings ); // Adding Heading of the table.
                 self::bkap_get_price_range_base_data( $product_id, $booking_settings ); // Adding default one hidden row in the table based on which we will add new ranges.
                 self::bkap_get_price_range_booking( $product_id, $booking_settings ); // Displaying the table based on the added ranges.
                 
                ?>
                
                <tr style="padding:5px; border-top:2px solid #eee">
                   <td colspan="<?php echo $attribute_count; ?>" style="border-right: 0px;">
                       <i>
                           <small><?php _e( 'Create block ranges and its per day and/or fixed price.', 'woocommerce-booking' ); ?></small>
                       <i>
                   </td>
                   <td colspan="4" align="right" style="border-left: none;">
                   <button type="button" class="button-primary bkap_save_price_range" onclick="bkap_save_price_ranges()"><i class="fa fa-floppy-o fa-lg"></i> <?php _e( 'Save' , 'woocommerce-booking' );?></button>
                   <button type="button" class="button-primary bkap_add_new_price_range"><i class="fa fa-plus" aria-hidden="true"></i> <?php _e( 'Add New Range' , 'woocommerce-booking' );?></button></td>
                </tr>
                
                </table>
            </div>
            <?php 
        }
        
        /**
         * Echoes the Column Headers for the Price Ranges table in 
         * Booking meta box->Block Pricing
         *
         * @param integer $product_id - Product ID
         * @param array $booking_settings - Booking Settings for the product.
         * @since 4.1.0
         **/

        static function bkap_get_price_range_booking_heading( $product_id, $booking_settings ){
            
            $currency_symbol     = get_woocommerce_currency_symbol();
            $product_attributes  = get_post_meta( $product_id, '_product_attributes', true );
            $width               = "";
            $count_attributes    = 0;
                            
            if ( is_array( $product_attributes ) && count( $product_attributes ) > 0 ) {
                $count_attributes = count( $product_attributes );
            }
            
            $count_attributes += 4;
            $available_width  = 90;
            
            $width_size       = ($available_width/$count_attributes);
            $width_size       = round($width_size, 2);
            $width            = 'width="'.$width_size.'%"';
            
            $product          = wc_get_product( $product_id );
            $product_type     = $product->get_type();
            
            ?>
            <tr>
            <?php 
                if ( $product_attributes != '' && $product_type == "variable" ) {
                    foreach ( $product_attributes as $k => $v )
                    {
                        $attribute_name = wc_attribute_label( $v["name"] );
                        ?>
                        <th <?php echo $width; ?>><?php _e( $attribute_name, 'woocommerce-booking' ); ?></th>
                    <?php
                    }
                }
                ?>
                <th <?php echo $width; ?>><?php _e( 'Minimum Day', 'woocommerce-booking' );?></th>
                <th <?php echo $width; ?>><?php _e( 'Maximum Day', 'woocommerce-booking' );?></th>
                <th <?php echo $width; ?>><?php _e( "Per Day ($currency_symbol)", 'woocommerce-booking' );?></th>
                <th <?php echo $width; ?>><?php _e( "Fixed ($currency_symbol)", 'woocommerce-booking' );?></th>
                
                <th width="4%" id="bkap_price_range_all_close" class="bkap_remove_all_price_ranges" style="text-align: center;cursor:pointer;"><i class="fa fa-trash" aria-hidden="true"></i></th>
            </tr>
            <?php
        }
        
        /**
         * Fixed Blocks table in Booking meta box->Block Pricing.
         *
         * @param integer $product_id - Product ID
         * @param array $booking_settings - Booking Settings for the product.
         * @since 4.1.0
         **/

        public static function bkap_fixed_block_booking_table( $product_id, $booking_settings ){
            
            $bkap_fixed_block_option     = get_post_meta( $product_id, '_bkap_fixed_blocks', true );
            $bkap_price_range_option     = get_post_meta( $product_id, '_bkap_price_ranges', true );
            
           
            
            $display_fixed_block_table   = "display:none";
            if( $bkap_fixed_block_option == "" && $bkap_price_range_option == "" ){
                $display_fixed_block_table = "";
            }
            
            $disable_block_pricing_class = "bkap_disable_block_pricing";
            
            if( isset( $bkap_fixed_block_option ) && $bkap_fixed_block_option == "booking_fixed_block_enable" ){
               $disable_block_pricing_class = "";
               $display_fixed_block_table   = "";
            }
            
        ?>
        
        <!-- Table for Fixed Block Booking -->
        <div class="bkap_fixed_block_booking <?php echo $disable_block_pricing_class; ?>" style="<?php echo $display_fixed_block_table; ?>">
        
            <div>
                <h4><?php _e( 'Fixed Blocks Booking :', 'woocommerce-booking' ); ?></h4>
            </div>
        
            <table id="bkap_fixed_block_booking_table" >
                <?php
                 // add date and time setup.
                 self::bkap_get_fixed_block_booking_heading( $product_id, $booking_settings ); 
                 self::bkap_get_fixed_block_booking_base_data( $product_id, $booking_settings );
                 self::bkap_get_fixed_block_booking( $product_id, $booking_settings );
                ?>
                
                <tr style="padding:5px; border-top:2px solid #eee">
                   <td colspan="3" style="border-right: 0px;">
                       <i>
                           <small><?php _e( 'Create fixed blocks of booking and its price.', 'woocommerce-booking' ); ?></small>
                       <i>
                   </td>
                   <td colspan="3" align="right" style="border-left: none;">
                   <button type="button" class="button-primary bkap_save_fixed_block" onclick="bkap_save_fixed_blocks()"><i class="fa fa-floppy-o fa-lg"></i> <?php _e( 'Save' , 'woocommerce-booking' );?></button>
                   <button type="button" class="button-primary bkap_add_new_fixed_block"><i class="fa fa-plus" aria-hidden="true"></i> <?php _e( 'Add New Block' , 'woocommerce-booking' );?></button></td>
                </tr>
            </table>
        
        </div>
        <?php   
        }
        
        /**
         * Echoes the Column Headers for the Fixed Blocks table in 
         * Booking meta box->Block Pricing
         *
         * @param integer $product_id - Product ID
         * @param array $booking_settings - Booking Settings for the product.
         * @since 4.1.0
         **/

        static function bkap_get_fixed_block_booking_heading( $product_id, $booking_settings ){
            $currency_symbol   = get_woocommerce_currency_symbol();
        ?>
            <tr>
                <th width="25%"><?php _e( 'Block Name', 'woocommerce-booking' );?></th>
                <th width="10%"><?php _e( 'Days', 'woocommerce-booking' );?></th>
                <th width="20%"><?php _e( 'Start Day', 'woocommerce-booking' );?></th>
                <th width="20%"><?php _e( 'End Day', 'woocommerce-booking' );?></th>
                <th width="20"><?php _e( "Price (".$currency_symbol.")", 'woocommerce-booking' );?></th>
                <td width="4%" id="bkap_fixed_block_all_close" class="bkap_remove_all_fixed_blocks" style="text-align: center;cursor:pointer;"><i class="fa fa-trash" aria-hidden="true"></i></th>
            </tr>
        <?php
        }
        
        /**
         * Adds a base table row for Booking meta box->Block Pricing->Fixed Blocks. 
         * Used when adding a new row to the table.
         *
         * @param integer $product_id - Product ID
         * @param array $booking_settings - Booking Settings for the product.
         * @global array $bkap_fixed_days array of weeekdays and its number 
         * @since 4.1.0
         **/
        static function bkap_get_fixed_block_booking_base_data( $product_id, $booking_settings ) {
            global $bkap_fixed_days;
        ?>
        
        <tr id="bkap_default_fixed_block_row" style="display: none;">
            <td width="25%">
                <input type="text" id="booking_block_name" name="booking_block_name" style="width:100%" placeholder="Enter Name of Block"></input>
            </td>
            <td width="10%">
                <input type="number" id="number_of_days" name="number_of_days" min=0 style="width:100%"></input>
            </td>
            <td width="20%">
                <select id="start_day" name="start_day" style="width:100%">
                    <?php 
                    $days = $bkap_fixed_days;
                    foreach ( $days as $dkey => $dvalue ) {
                    ?>
                    <option value="<?php echo $dkey; ?>"><?php echo $dvalue; ?></option>
                    <?php 
                    }
                    ?>
                </select>
            </td>
            <td width="20%">
                <select id="end_day" name="end_day" style="width:100%">
                <?php 
                foreach ( $days as $dkey => $dvalue ) {
                    ?>
                    <option value="<?php echo $dkey; ?>"><?php echo $dvalue; ?></option>
                    <?php 
                }
                ?>
                </select>
             </td>
            <td width="20%"><input type="text" id="fixed_block_price" name="fixed_block_price" style="width:100%" placeholder="Block Price"></input></td>
            
            <td width="4%" id="bkap_fixed_block_close" class="" style="text-align: center;cursor:pointer;"><i class="fa fa-trash" aria-hidden="true"></i></td>
        </tr>
        
        <?php 
        }
        
        /**
         * Adds a base table row for Booking meta box->Block Pricing->Price Ranges. 
         * Used when adding a new row to the table.
         *
         * @param integer $product_id - Product ID
         * @param array $booking_settings - Booking Settings for the product.
         * @since 4.1.0
         **/

        static function bkap_get_price_range_base_data( $product_id, $booking_settings ) {
            
            $product_attributes    = get_post_meta( $product_id, '_product_attributes', true );
            $i                     = 1;
            $j                     = 1;
            $product               = wc_get_product( $product_id );
            $product_type          = $product->get_type();
            
            ?>
            <tr id="bkap_default_price_range_row" style="display: none;">
            <?php 
            if( $product_attributes != '' && $product_type == "variable" ) {
                
                foreach( $product_attributes as $key => $value ) {
                    
                        if ( $value['is_taxonomy'] ) {
                            $value_array = wc_get_product_terms( $product_id, $value['name'], array( 'fields' => 'names' ) );
                        }else{
                            $value_array  =  explode( ' | ', $value['value'] );
                        }
                            
                        print( '<td><select name="attribute_'.$i.'" id="attribute_'.$i.'" value="" style="width:100%">' );
            
                        $j            =  1;
            
                        foreach( $value_array as $k => $v ) {
                            $attr_value  = trim( $v );
                            print( '<option name="option_attribute_'.$i.'_'.$j.'" id="option_attribute_'.$i.'_'.$j.'" value="'.htmlspecialchars( $attr_value ).'">'.$v.'</option>' );
                        }
                        
                        print( '</select></td>' );
                        $i++;
                        $j++;                      
                }
            }
            ?>
                <td>
                    <input type="number" id="number_of_start_days" name="number_of_start_days" min=0 style="width:100%"></input>
                </td>
                <td>
                    <input type="number" id="number_of_end_days" name="number_of_end_days" min=0 style="width:100%"></input>
                </td>
                <td >
                    <input type="text" id="per_day_price" name="per_day_price" style="width:100%"></input>
                </td>
                <td>
                    <input type="text" id="fixed_price" name="fixed_price" style="width:100%"></input>
                </td>
                
                <td width="4%" id="bkap_price_range_close" class="" style="text-align: center;cursor:pointer;"><i class="fa fa-trash" aria-hidden="true"></i></td>
            </tr>
        
        <?php 
        }
        
        /**
         * Displays the existing Price Ranges in Booking meta box->Block Pricing->Price Ranges
         * as per the saved data.
         *
         * @param integer $product_id - Product ID
         * @param array $booking_settings - Booking Settings for the product.
         * @since 4.1.0
         **/

        static function bkap_get_price_range_booking( $product_id, $booking_settings ){
            
            $result      = get_post_meta( $product_id, '_bkap_price_range_data', true );
            $results     = ( isset( $result ) && $result != "" ) ? $result : array();
            $block_count = count( $results );
            $row_number  = 0;
            $max_key     = 0;
            $_product    = wc_get_product( $product_id );
            
            if( $block_count != 0 ){
                $max_key     = max( array_keys( $results ) );
            }
            
            
            $product_attributes = "";
            if( $_product->is_type( 'variable' ) ){
                
                $product_attributes      = get_post_meta( $product_id, '_product_attributes', true );
                
                if( $product_attributes != "" )
                $product_attributes_keys = array_keys( $product_attributes );
            }
            
            $c = 0;
            while( $row_number <= $max_key ) {
                
                if( !in_array( $row_number, array_keys( $results ) ) ){
                    $row_number++;
                    continue;
                }
                
                $i = 0;
                $number_of_columns = count( $results[$row_number] );
                $min_number = $max_number = $per_day_price = $fixed_price = "";
                 
                $min_number          =   $results[$row_number]['min_number'];
                $max_number          =   $results[$row_number]['max_number'];
                $per_day_price       =   $results[$row_number]['per_day_price'];
                $fixed_price         =   $results[$row_number]['fixed_price'];
                
                $bkap_row_toggle         = '';
                $bkap_row_toggle_display = '';
                 
                if( $c > 4 ){
                    $bkap_row_toggle = "bkap_price_range_row_toggle";
                    $bkap_row_toggle_display = 'style="display:none;"';
                }
                
                
                ?>
                <tr id="bkap_price_range_row_<?php echo $row_number; ?>" class="<?php echo $bkap_row_toggle; ?>" <?php echo $bkap_row_toggle_display; ?>>
                <?php 
                    if( $product_attributes != '' && $_product->is_type( 'variable' ) ) {
                        
                        foreach( $product_attributes as $key => $value ) {
                           
                                if ( $value['is_taxonomy'] ) {
                                    $value_array = wc_get_product_terms( $product_id, $value['name'], array( 'fields' => 'names' ) );
                                }else{
                                    $value_array  =  explode( ' | ', $value['value'] );
                                }
                                    
                                print( '<td><select name="attribute_'.$i.'_'.$row_number.'" id="attribute_'.$i.'_'.$row_number.'" value="" style="width:100%">' );
                    
                                $j            =  1;
                    
                                foreach( $value_array as $k => $v ) {
                                    $attr_value      = trim( $v );
                                    $attribute_key   = $product_attributes_keys[$i];
                                    $selected        = "";
                                
                                    if( $attr_value == $results[$row_number][$attribute_key] ){
                                      $selected = "selected";  
                                    }
                                    print( '<option name="option_attribute_'.$i.'_'.$j.'" id="option_attribute_'.$i.'_'.$j.'" value="'.htmlspecialchars( $attr_value ).'" '.$selected.'>'.$v.'</option>' );
                                }
                                
                                print( '</select></td>' );
                                $i++;
                                $j++;                      
                        }
                    }
                    ?>
                    <td>
                        <input type="number" id="number_of_start_days_<?php echo $row_number; ?>" name="number_of_start_days" min=0 style="width:100%" value="<?php echo $min_number; ?>"></input>
                    </td>
                    <td>
                        <input type="number" id="number_of_end_days_<?php echo $row_number; ?>" name="number_of_end_days" min=0 style="width:100%" value="<?php echo $max_number; ?>"></input>
                    </td>
                    <td >
                        <input type="text" id="per_day_price_<?php echo $row_number; ?>" name="per_day_price" style="width:100%" value="<?php echo $per_day_price; ?>"></input>
                    </td>
                    <td>
                        <input type="text" id="fixed_price_<?php echo $row_number; ?>" name="fixed_price" style="width:100%" value="<?php echo $fixed_price; ?>"></input>
                    </td>
                    
                    <td width="4%" id="bkap_price_range_close_<?php echo $row_number; ?>" class="" style="text-align: center;cursor:pointer;"><i class="fa fa-trash" aria-hidden="true"></i></td>
                </tr>
            
                <?php 
                
                
                $row_number++;
                $c++;
            }
            
            if ( $block_count > 5 ){
            ?>
            <tr style="">
               <td colspan="<?php echo $number_of_columns + 1 ; ?>">
               <span class="bkap_expand-close">
                   <a href="#" class="bkap_price_range_expand_all"><?php echo __( 'Expand', 'woocommerce-booking' ); ?></a> / <a href="#" class="bkap_price_range_close_all"><?php echo __( 'Close', 'woocommerce-booking' ); ?></a>
               </span>
               </td>
            </tr>
            <?php
            }
            
        }
        
        /**
         * Displays the existing Fixed Blocks in Booking meta box->Block Pricing->Fixed Blocks
         * as per the saved data.
         *
         * @param integer $product_id - Product ID
         * @param array $booking_settings - Booking Settings for the product.
         * @global array $bkap_fixed_days Array of weekdays and its numbers
         * @global object $post WP_Post
         * @global object $wpdb Global wpdb object
         *
         * @since 4.1.0
         **/

        static function bkap_get_fixed_block_booking( $product_id, $booking_settings ) {
            global $bkap_fixed_days;
            
            global $post, $wpdb;
            
            $post_id = 0;
            
            if ( isset( $post->ID ) ) {
                $post_id = $post->ID;
            }
            
            $duplicate_of     =   bkap_common::bkap_get_product_id( $post_id );
            
            if ( isset( $duplicate_of ) ) $post_id = $duplicate_of;
            
            $result      = get_post_meta( $post_id, '_bkap_fixed_blocks_data', true );
            $results     = ( isset( $result ) && $result != "" ) ? $result : array();
            $block_count = count( $results );
            $max_key     = 0;
            
            if( $block_count != 0 ){
                $max_key     = max( array_keys( $results ) );
            }
             
            $row_number = 0;
            $i = 0;
            
            while( $row_number <= $max_key ) {
                
                if( !in_array( $row_number, array_keys( $results ) ) ){
                    $row_number++;
                    continue;
                }
                
                $block_name = $number_of_days = $start_day = $end_day = $price = "";
                
                $block_name      =   $results[$row_number]['block_name'];
                $number_of_days  =   $results[$row_number]['number_of_days'];
                $start_day       =   $results[$row_number]['start_day'];
                $end_day         =   $results[$row_number]['end_day'];
                $price           =   $results[$row_number]['price'];
                
                
                $bkap_row_toggle         = '';
                $bkap_row_toggle_display = '';
                
                if( $i > 4 ){
                    $bkap_row_toggle = "bkap_fixed_row_toggle";
                    $bkap_row_toggle_display = 'style="display:none;"';
                }
                
            ?>
                    
            <tr id="bkap_fixed_block_row_<?php echo $row_number; ?>" class="<?php echo $bkap_row_toggle;?>" <?php echo $bkap_row_toggle_display;?>>
                <td width="25%">
                    <input type="text" id="booking_block_name_<?php echo $row_number; ?>" name="booking_block_name" style="width:100%" placeholder="Enter Name of Block" value="<?php echo $block_name;?>"></input>
                </td>
                <td width="10%">
                    <input type="number" id="number_of_days_<?php echo $row_number; ?>" name="number_of_days" min=0 style="width:100%" value="<?php echo $number_of_days;?>"></input>
                </td>
                <td width="20%">
                
                    <select id="start_day_<?php echo $row_number; ?>" name="start_day" style="width:100%">
                        <?php 
                        $days = $bkap_fixed_days;
                        foreach ( $days as $dkey => $dvalue ) {
                            $start_selected = "";
                            //echo gettype( $dkey ) . ' - ' . gettype( $start_day );
                            if( (string)$dkey == (string)$start_day ) {
                                $start_selected = "selected";
                            }
                            ?><option value="<?php echo $dkey; ?>" <?php echo $start_selected; ?>><?php echo $dvalue; ?></option> <?php 
                        }
                        ?>
                    </select>
                </td>
                <td width="20%">
                    <select id="end_day_<?php echo $row_number; ?>" name="end_day" style="width:100%">
                    <?php 
                    foreach ( $days as $dkey => $dvalue ) {
                        $end_selected = "";
                        if( (string)$dkey == (string)$end_day ){
                          $end_selected = "selected";
                          
                        }
                        ?><option value="<?php echo $dkey; ?>" <?php echo $end_selected; ?>><?php echo $dvalue; ?></option> <?php
                    }
                    ?>
                    </select>
                 </td>
                <td width="20%"><input type="text" id="fixed_block_price_<?php echo $row_number; ?>" name="fixed_block_price" style="width:100%" placeholder="Block Price" value="<?php echo $price; ?>"></input></td>
                
                <td width="4%" id="bkap_fixed_block_close_<?php echo $row_number; ?>" class="" style="text-align: center;cursor:pointer;"><i class="fa fa-trash" aria-hidden="true"></i></td>
            </tr>
        
            <?php
            $row_number++;
            $i++;
            }

            if ( $block_count > 5 ){
            ?>
            <tr style="">
               <td colspan="6">
               <span class="bkap_expand-close">
                   <a href="#" class="bkap_fixed_expand_all"><?php echo __( 'Expand', 'woocommerce-booking' ); ?></a> / <a href="#" class="bkap_fixed_close_all"><?php echo __( 'Close', 'woocommerce-booking' ); ?></a>
               </span>
               </td>
            </tr>
            <?php
                }
        }
            
        /**
        * This function is used to clculate and show the booking price 
        * when Fixed Blocks are used
        * 
        * @param integer $product_id  - Product ID
        * @param string $product_type - Product Type
        * @param integer $variation_id - Variation ID, 0 for simple products
        * @param string $checkin_date - Booking start date
        * @param string $checkout_date - Booking end date
        * @param string $currency_selected - Currency such as USD, AUD etc.
        * 
        * @hook bkap_display_multiple_day_updated_price
        * @since 4.1.0
        */

        function bkap_fixed_block_show_updated_price( $product_id, $product_type, $variation_id, $checkin_date, $checkout_date, $currency_selected ) {
            
            if ( !isset( $_POST['variable_blocks'] ) || ( isset( $_POST['variable_blocks'] ) && $_POST['variable_blocks'] != 'Y' ) ) {
                
                $duplicate_of     =   bkap_common::bkap_get_product_id( $product_id );
                $booking_settings =   get_post_meta( $duplicate_of, 'woocommerce_booking_settings', true );
                
               // $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
                
                if ( $product_type == 'variable' ) {
                    
                    if ( $variation_id != '' ) {
                        $price = bkap_common::bkap_get_price( $product_id, $variation_id, $product_type, $checkin_date, $checkout_date );

                        if ( isset( $booking_settings['booking_fixed_block_enable'] ) && $booking_settings['booking_fixed_block_enable']  == "booking_fixed_block_enable" ) {
                            $price += $_POST['block_option_price'];
                        }
                    }
                    else {
                        $price = 0;
                    }
                } else {
                    
                    if ( isset( $booking_settings['booking_fixed_block_enable'] ) && $booking_settings['booking_fixed_block_enable']  == "booking_fixed_block_enable" ) {
                        $price = $_POST['block_option_price'];
                        
                    }else {
                        $price = bkap_common::bkap_get_price( $product_id, $variation_id, $product_type );
                    }
                }
            }else {
                
                if ( isset( $_POST['price'] ) ) {
                    $price = $_POST['price'];
                }
                
            }
            $error_message = __( "Please select an option.", "woocommerce-booking" );
            if ( function_exists( 'is_bkap_deposits_active' ) && is_bkap_deposits_active() || function_exists( 'is_bkap_seasonal_active' ) && is_bkap_seasonal_active() ) {
                if ( isset( $price ) && $price != '' ) {
                    $_POST['price'] = $price;
                }
                else {
                    print( 'jQuery( "#show_time_slot" ).html( "' . addslashes( $error_message ) . '");' );
                    die();
                }
            }
            else {
                
                if ( isset( $price ) && $price !== '' ) {
                    $_POST['price'] = $price;
                }
                else {
                    print( 'jQuery( "#show_time_slot" ).html( "' . addslashes( $error_message ) . '");' );
                    die();
                }
            }
        }
            
        /**
         * This function is used to clculate and show the booking price
         * when Price Ranges are used.
         *
         * @param integer $product_id  - Product ID
         * @param string $product_type - Product Type
         * @param integer $variation_id - Variation ID, 0 for simple products
         * @param string $checkin_date - Booking start date
         * @param string $checkout_date - Booking end date
         * @param string $currency_selected - Currency such as USD, AUD etc.
         *
         * @hook bkap_display_multiple_day_updated_price
         * @since 4.1.0
         */

        public function bkap_price_range_show_updated_price( $product_id, $product_type, $variation_id_to_fetch, $checkin_date, $checkout_date, $currency_selected ) {
            
            $variation_id       =   $variation_id_to_fetch;
            $number_of_days     =   strtotime( $checkout_date ) - strtotime( $checkin_date );
            
            $booking_settings   =   get_post_meta( $product_id, 'woocommerce_booking_settings', true );
            $number             =   floor( $number_of_days/( 60 * 60 * 24 ) );
            
            if ( isset( $booking_settings['booking_charge_per_day'] ) && $booking_settings['booking_charge_per_day'] == 'on' ){
                $number = $number + 1;
            }
                
            if( $number == 0 && isset( $booking_settings['booking_same_day'] ) && $booking_settings['booking_same_day'] == 'on' ) {
                $number = 1;
            }
            
            if ( $number <= 0 ) {
                $number = 1;
            }
            
            if ( $product_type == 'variable' ) {
                $variations_selected   = array();
                $string_explode        = '';
                $product_attributes    = get_post_meta( $product_id, '_product_attributes', true );
                $i                     = 0;
                
                foreach( $product_attributes as $key => $value ) {
                    
                    if( isset( $_POST['attribute_selected'] ) ) {
                        $string_explode = explode( "|", $_POST['attribute_selected'] );
                    }else{
                        $string_explode = array();
                    }
                    
                    if ( $value['is_taxonomy'] ) {
                        $value_array = wc_get_product_terms( $product_id, $value['name'], array( 'fields' => 'names' ) );
                    }else{
                        $value_array  =  explode( ' | ', $value['value'] );
                    }
                    
                    $s_value      = '';
                    
                    foreach( $string_explode as $sk => $sv ) {
                        
                        if( $sv == '' ){
                            unset( $string_explode[ $sk ] );
                        }
                    }
                
                    foreach( $value_array as $k => $v ){
                        $string1 = str_replace(" ", "", $v );
                        
                        if(count($string_explode) > 0) {
                            $string2 = str_replace( " ", "", $string_explode[ $i+1 ]);
                        } else {
                            $string2 = '';
                        }
                        
                        if( strtolower( addslashes( $string1 ) ) == strtolower( $string2 ) /* $pos_value != 0*/) {
                
                            if( substr( $v, 0, -1 ) === ' ' ) {
                                $result                    = rtrim( $v, " " );
                                $variations_selected[$i]   = $result;
                            }
                            
                            if( substr( $v, 0, 1 ) === ' ' ) {
                                $result                    = preg_replace( "/ /", "", $v, 1 );
                                $variations_selected[$i]   = addslashes( $result );
                            } else {
                                $variations_selected[$i] = addslashes( $v );
                            }
                        }
                    }
                    $i++;
                }
            }
            else {
                $variations_selected = array();
            }
            
            $price = $this->price_range_calculate_price( $product_id, $product_type, $variation_id, $number, $variations_selected );
            
            if ( function_exists('is_bkap_deposits_active') && is_bkap_deposits_active() || function_exists('is_bkap_seasonal_active') && is_bkap_seasonal_active() ) {
                
                if ( isset( $price ) && $price != '' ) {
                    
                    if ( isset( $_POST['variable_blocks'] ) && $_POST['variable_blocks'] == 'Y' ) {
                        $_SESSION['variable_block_price'] = $_POST['price'] = $price;
                    }else {
                        $_SESSION['variable_block_price'] =  $_POST['price'] = '';
                    }
                }else {
                    $error_message = __( "Please select an option.", "woocommerce-booking" );
                    print( 'jQuery( "#show_time_slot" ).html( "' . addslashes( $error_message ) . '");' );
                    die();
                }
            }else {
                
                if ( isset( $_POST['variable_blocks'] ) && $_POST['variable_blocks'] == 'Y' ) {
                    $_SESSION['variable_block_price'] = $price;
                    $_POST['price']                   = $price;
                }else {
                    $_SESSION['variable_block_price'] =  $_POST['price'] = '';
                }
                
            }
            
        }
        
        /**
         * Returns the booking price applicable when Price Ranges is enabled.
         * 
         * @param $product_id - Product ID
         * @param string $product_type - Product Type
         * @param integer $variation_id - Variation ID, 0 for simple products.
         * @param integer $number - Number of nights being booked
         * @param array $variations_selected - Attributes selected and their values.
         *
         * @since 4.1.0
         *
         * @return string $price Price based on the price by range
         */

        public static function price_range_calculate_price( $product_id, $product_type, $variation_id, $number, $variations_selected ) {
            
            $price               = 0;
            $results_price       = array();
            $price_range         = get_post_meta( $product_id, '_bkap_price_ranges', true );
            $price_range_data    = get_post_meta( $product_id , '_bkap_price_range_data', true );
            $_product            = wc_get_product( $product_id );
            
           
            if ( $product_type == 'variable' ) {
                
                if( $price_range == "booking_block_price_enable" && $price_range_data != "" ){
                    
                    $bkap_post                        = $_POST; // Getting all the data from 
                    $attribute_string            = "attribute_";
                    $attribute_variation_pair    = array();
                    
                    // Preparing array for attribute and its value
                    // so that in post meta array we can check for that combination 
                    foreach( $bkap_post as $post_key => $post_value ){
                        
                        if( strpos( $post_key, $attribute_string ) !== false ) {
                            
                            $p_key = str_replace( $attribute_string,"", $post_key );
                            $attribute_variation_pair[ $p_key ] = $post_value;
                        }
                    }
                    
                    // Removing attribute_selected from the attribute array so we got the final array.
                    array_shift( $attribute_variation_pair ); 

                    // Modify the array to ensure Attribute names are used instead of slugs.
                    foreach( $attribute_variation_pair as $a_name => $a_slug ) {
                        $term = get_term_by('slug', $a_slug, $a_name );
                        if( false !== $term )
                            $attribute_variation_pair[ $a_name ] = $term->name;
                    }
                    
                    $_POST['fixed_price'] = $_POST['variable_blocks'] = "N";
                    
                    $result_array = $results =  array();
                    
                    // Looping through post meta and checking if there is range available
                    // for selected variation and min max numbers of days.
                    
                    $e = 0;
                    
                    foreach( $price_range_data as $price_range_data_key => $price_range_data_value ){
                        
                        $i = 0;
                        $a_v_p_count = count( $attribute_variation_pair ); // 
                        
                        /*
                         * Looping through $attribute_variation_pair and setting $i
                         * so if the count of $attribute_variation_pair and $i is equal
                         * then combination is found in post meta for selected attributes
                         */
                        
                        foreach( $attribute_variation_pair as $a_v_p_key => $a_v_p_value ){
                            if( array_key_exists( $a_v_p_key, $price_range_data_value ) && $price_range_data_value[ $a_v_p_key ] == stripslashes( $a_v_p_value ) ) {
                                $i++; 
                            }
                        }
                        
                        /*
                         * Checking if the combination matched and selected number of days for booking
                         * is falling in min and max numbers set in range or not.
                         * If set then assign that array in $result_price
                         */
                        
                        if( $i == $a_v_p_count && $price_range_data_value['min_number'] <= $number && $price_range_data_value['max_number'] >= $number ){
                            
                            $results_price[$e] = $price_range_data_value; // here we got the range for the selection.
                        }  
                        $e++;
                    }
                    
                    // If we found the ranges in _bkap_price_range_data. 
                    if ( isset( $results_price ) && count( $results_price ) > 0 ) {
                        
                        // Looping throught the ranges and calculating the price.
                        foreach( $results_price as $k => $v ){
                        
                            if ( !empty( $results_price[ $k ] ) ){
                                $_POST['variable_blocks'] = "Y";
                                    
                                if ( $variation_id != '' ) {
                                    
                                    $price = get_post_meta( $variation_id, '_sale_price', true );
                        
                                    if ( !isset( $price ) || $price == '' || $price == 0 ){
                                        $price = get_post_meta( $variation_id, '_regular_price', true );
                                    }
                                
                                    if ( $v['fixed_price'] != 0 ){
                                        $calc_price           = get_standard_decimal_thousand_separator_price( $v['fixed_price'] );
                                        $price                = $calc_price . "-fixed";
                                        $_POST['fixed_price'] = "Y";
                                    } else {

                                        $calc_price   = get_standard_decimal_thousand_separator_price( $v['per_day_price'] );
                                        $calc_price   = $calc_price * $number;
                                        $price        = $calc_price . "-per_day";
                                    }
                                    
                                }else {
                                    $price = 0;
                                }
                            } else {
                                unset( $results_price[ $k ] );
                            }
                        }
                        
                    } else { 
                        
                        /* You will come here when number of days selected for booking do not have any matching range in data
                         * then see the last matching range based on nearest max day in other range.
                         */
                        
                        // Finding the ranges which have max days lower than selected number of days for booking.
                        foreach( $price_range_data as $price_range_data_key => $price_range_data_value ){
                        
                            $i = 0;
                            $a_v_p_count = count( $attribute_variation_pair );
                        
                            foreach( $attribute_variation_pair as $a_v_p_key => $a_v_p_value ){
                                if( array_key_exists( $a_v_p_key, $price_range_data_value ) && $price_range_data_value[ $a_v_p_key ] == stripslashes( $a_v_p_value ) ) {
                        
                                    $i++;
                                }
                            }
                        
                            if( $i == $a_v_p_count && $price_range_data_value['min_number'] <= $number ){
                                 
                                $results_price[$e] = $price_range_data_value;
                            }
                            $e++;
                        }
                        
                        // In $results_price array all the ranges will be added which 
                        // have lower max days compare to selected days for booking.
                        
                        /* 
                         * Sorting the array so the price of the range will be calculated correctly.
                         * Issue was occurring when range of days not added sequencially
                         */
                        
                        foreach ( $results_price as $kk => $val ) {
                            $ab[ $kk ] =  $val['max_number'];
                        }
                        
                        asort( $ab );
                        $ordered = array();
                        
                        foreach ( $ab as $key => $value ) {
                            if ( array_key_exists( $key, $results_price ) ) {
                                $ordered[ $key ] = $results_price[ $key ];
                                unset( $results_price[ $key ] );
                            }
                        }
                        
                        $results_price = $ordered; // Here we are gettting sorted array for $result_price
                                                    
                        // Looping throught the ranges and calculating the price.
                        foreach ( $results_price as $k => $v ) {
                                
                            if ( !empty( $results_price[ $k ] ) ) {
                                
                                $_POST['variable_blocks'] = "Y";
                        
                                if ( $variation_id != '' ) {
                                    $price = get_post_meta( $variation_id, '_sale_price', true );
                                        
                                    if ( !isset( $price ) || $price == '' || $price == 0 ){
                                        $price = get_post_meta( $variation_id, '_regular_price', true );
                                    }
                                        
                                    $diff_days = '';
                                        
                                    if ( $v['max_number'] < $number ){
                                        $diff_days = $number - $v['max_number'];
                        
                                        if ( $v['fixed_price'] != 0 ){
                                            $_POST['fixed_price']  = "Y";
                                            $calc_price            = $v['fixed_price'] + ( $price * $diff_days );
                                            $price                 = $calc_price .  "-fixed";
                                        } else {
                                            $calc_price            = ($v['per_day_price'] * $v['max_number']) + ($price * $diff_days);
                                            $price                 = $calc_price . "-per_day";
                                        }
                        
                                    } else {
                        
                                        if ( $v['fixed_price'] != 0 ) {
                                            $_POST['fixed_price']  = "Y";
                                            $calc_price            = get_standard_decimal_thousand_separator_price( $v['fixed_price'] );
                                            $price                 = $calc_price . "-fixed";
                                        } else {
                                            $calc_price            = get_standard_decimal_thousand_separator_price( $v['per_day_price'] );
                                            $calc_price            = $calc_price * $number;
                                            $price                 = $calc_price . "-per_day";
                                        }
                                    }
                                }
                                else {
                                    $price = 0;
                                }
                            } else {
                                unset( $results_price[ $k ] );
                            }
                        }
                        
                        // If no result found then simply set price as variation price.
                        if ( count( $results_price ) == 0 ) {
                            $price = 0;
                            
                            if ( $variation_id != '' ) {
                                $price = get_post_meta( $variation_id, '_sale_price', true );
                                
                                if ( !isset( $price ) || $price == '' || $price == 0) {
                                    $price = get_post_meta( $variation_id, '_regular_price', true );
                                }
                                $price .= "-";
                            }
                        }
                    }
                } else { // price by range is not enable and no record in the price by range data.
                    if ( $variation_id != '' ) {
                        $price = get_post_meta( $variation_id, '_sale_price', true );
                        
                        if ( !isset( $price ) || $price == '' || $price == 0 ){
                            $price = get_post_meta( $variation_id, '_regular_price', true );
                        }
                        $price .= "-";
                    }
                    else {
                        $price = 0;
                    }
                }
                // Code for variable product ends here
            } else {
                
                if( $price_range == "booking_block_price_enable" ){
                    // Price by range is active.
                    
                    $results_price = array();
                    
                    foreach( $price_range_data as $price_range_data_key => $price_range_data_value ){
                        
                        if( $price_range_data_value['min_number'] <= $number && $price_range_data_value['max_number'] >= $number ){
                            $results_price[$price_range_data_key] = $price_range_data[$price_range_data_key];
                        }
                    }
                    
                    if ( count( $results_price ) == 0 ) {
                        // if no records found in $result price array then 
                        
                        foreach( $price_range_data as $price_range_data_key => $price_range_data_value ){
                             
                            if( $price_range_data_value['min_number'] <= $number ){
                                $results_price[$price_range_data_key] = $price_range_data[$price_range_data_key];
                            }
                        }
                        
                        if ( count( $results_price ) == 0 ) {
                              $price = $_product->get_price();
                              $price .= "-";
                              
                        }else{
                            
                            // select max value from the range 
                            $max_results_block  = array();
                            foreach( $price_range_data as $price_range_data_key => $price_range_data_value ){
                                if( $price_range_data_value['min_number'] <= $number ){
                                    array_push( $max_results_block , $price_range_data_value['max_number'] );
                                }
                            }
                            
                            $results_block['maximum_number_of_days'] = max( $max_results_block ); // Max day in all the range.
                            
                            $results_price = array();
                            
                            foreach( $price_range_data as $price_range_data_key => $price_range_data_value ){
                                 
                                 if( $price_range_data_value['min_number'] <= $number && $price_range_data_value['max_number'] == $results_block['maximum_number_of_days'] ){
                                     $results_price[$price_range_data_key] = $price_range_data[$price_range_data_key];
                                 }
                            }
                            
                            foreach( $results_price as $k => $v ) {
                            
                                if ( !empty( $results_price[ $k ] ) ) {
                                    
                                    $_POST['variable_blocks'] = "Y";
                                    $price = $_product->get_price();
                            
                                    if ( $results_block['maximum_number_of_days'] < $number ) {
                                        $diff_days = $number - $results_block['maximum_number_of_days'];
                                        
                                        if ( $v['fixed_price'] != 0 ) {
                                            $_POST['fixed_price']   = "Y";
                                            $calc_price             = $v['fixed_price'] + ( $price * $diff_days );
                                            $price                  = $calc_price . "-fixed";
                                        } else {
                                            $calc_price                 = get_standard_decimal_thousand_separator_price( $v['per_day_price'] );
                                            $calc_price = ( $calc_price * $results_block['maximum_number_of_days'] ) + ( $price * $diff_days );
                                            $price      = $calc_price . "-per_day";
                                        }
                                    }
                                }else {
                                    unset( $results_price[ $k ] );
                                }
                            }
                        }
                    }else{
                        
                        foreach ( $results_price as $k => $v ) {
                        
                            if( !empty( $results_price[ $k ] ) ) {
                                
                                $_POST['variable_blocks'] = "Y";
                                    
                                if ( $v['fixed_price'] != 0 ) {
                                    
                                    $_POST['fixed_price'] = "Y";
                                    $price                = get_standard_decimal_thousand_separator_price( $v['fixed_price'] );
                                    $price               .= "-fixed";
                                    $price               .= "-";
                                } else {

                                    $dtsprice = get_standard_decimal_thousand_separator_price( $v['per_day_price'] );
                                    
                                    $price    = $dtsprice * $number;

                                    
                                    //$price = number_format ( $price, wc_get_price_decimals(), $decimal_separator, $thousand_separator );
                                    $price               .= "-per_day";
                                    $price               .= "-";
                                }
                            } else {
                                unset( $results_price[ $k ] );
                            }
                        }
                    }
                     
                    
                }else {
                    // Price by range is not active.
                    $price = $_product->get_price();
                    $price .= "-";
                    
                }
            }
            
            return $price;
        }
            
            
        /**
        * This function return the count of Fixed Blocks present for a product.
        * 
        * @param integer $post_id - Product ID
        * @return integer Count of Blocks present.
        * 
        * @since 4.1.0
        */

        static function bkap_get_fixed_blocks_count( $post_id ) {
            global $wpdb;
            
            $result    = get_post_meta( $post_id, '_bkap_fixed_blocks_data', true );
            $results   = ( isset( $result ) && $result != "" ) ? $result : array();
            
            return count( $results );
        }
            
        /**
         * Returns the Fixed Blocks present for a product.
         * 
         * @param integer $post_id - Product ID
         * @return array Fixed Blocks for the product.
         * 
         * @since 4.1.0
         */

        static function bkap_get_fixed_blocks( $post_id ) {
            global $wpdb;
            
            $result    = get_post_meta( $post_id, '_bkap_fixed_blocks_data', true );
            $results   = ( isset( $result ) && $result != "" ) ? $result : array();
        
            return $results;
        }
            
        /**
         * This function will return translated string for the Fixed Block names.
         * 
         * @param string $get_translated_text 
         * @param string $message - Message being translated
         * @param string $language - Language to be translated to
         * @return string $translated - Translated Data
         * @since 4.1.0
         **/    

        function bkap_get_translated_texts ( $get_translated_text, $message, $language ) {
                
                if (function_exists('icl_register_string')) {
                    if ( $language == 'en' ) {
                        return $message;
                    } else {
                        global $wpdb;
                        $context = 'woocommerce-booking';
                        $translated = '';
                        $results = $wpdb->get_results($wpdb->prepare("
                            SELECT s.name, s.value, t.value AS translation_value, t.status
                            FROM  {$wpdb->prefix}icl_strings s
                            LEFT JOIN {$wpdb->prefix}icl_string_translations t ON s.id = t.string_id
                            WHERE s.context = %s
                            AND (t.language = %s OR t.language IS NULL)
                            ", $context, $language), ARRAY_A);
                        
                            foreach ($results as $each_entry) {
                                    if ($each_entry['name'] == $get_translated_text) {
                                        if ($each_entry['translation_value']) {
                                            $translated = $each_entry['translation_value'];
                                        } else {
                                            $translated = $each_entry['value'];
                                        }
                                    }
                            }
                            return $translated;
                     }
                  }else {
                      return $message;
                  }
          }
          
          /**
           * This function will ensure that when a new product
           * is created as a duplicate of an existing one, then
           * the Pricing Options (Fixed Blocks and Price Ranges)
           * are also copied alongwith the other settings.
           * 
           * @param integer $new_id - ID of the Product being created
           * @param integer $old_id - ID of the product being copied from.
           * @hook bkap_product_addon_duplicate
           * @since 4.1.0
           */

          function price_range_product_duplicate( $new_id, $old_id ) {
              
              $price_range        =   get_post_meta( $old_id, '_bkap_price_ranges', true );
              $price_range_data   =   get_post_meta( $old_id, '_bkap_price_range_data', true );
              
              $fixed_block        =   get_post_meta( $old_id, '_bkap_fixed_blocks', true );
              $fixed_block_data   =   get_post_meta( $old_id, '_bkap_fixed_blocks_data', true );
              
              update_post_meta( $new_id, "_bkap_price_ranges", $price_range );
              update_post_meta( $new_id, "_bkap_price_range_data", $price_range_data );
              
              update_post_meta( $new_id, "_bkap_fixed_blocks", $fixed_block );
              update_post_meta( $new_id, "_bkap_fixed_blocks_data", $fixed_block_data );
              
          }
          
          /**
           * This function adds hidden fields on the frontend
           * product page if Price Ranges are enabled.
           * 
           * @param $product_id - Product ID
           * 
           * @hook bkap_before_booking_form
           * @since 4.1.0
           */
          
          function bkap_price_range_booking_after_add_to_cart( $product_id ) {
              
              global $post, $wpdb;
          
              //$bakp_price_range = get_post_meta( $post->ID, '_bkap_price_ranges', true );
              $bakp_price_range = get_post_meta( $product_id, '_bkap_price_ranges', true );
          
              if ( isset( $bakp_price_range ) && $bakp_price_range == 'booking_block_price_enable' ) {
                  
                  echo ' <input type="hidden" id="block_option_enabled_price"  name="block_option_enabled_price" value="on"/>';
                  //    echo ' <input type="hidden" id="block_variable_option_price"  name="block_variable_option_price" value=""/>';
                  echo ' <input type="hidden" id="wapbk_variation_value"  name="wapbk_variation_value" value=""/>';
              } else {
                  echo ' <input type="hidden" id="block_option_enabled_price"  name="block_option_enabled_price" value=""/>';
              }
          
          }
            
        }
    }
    $bkap_block_booking = new bkap_block_booking();
 
?>
API documentation generated by ApiGen