Advertisement
palsushobhan

wcfm-appintment-booking-based-on-vacation

Jun 14th, 2022
1,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.04 KB | None | 0 0
  1. add_filter('wc_appointments_find_scheduled_day_slots', function($args, $product) {
  2.     $vendor_id = wcfm_get_vendor_id_by_post($product->get_id());
  3.     if(!$vendor_id) return $args;
  4.     $vendor_data = get_user_meta( $vendor_id, 'wcfmmp_profile_settings', true );
  5.     $vacation_mode = isset( $vendor_data['wcfm_vacation_mode'] ) ? $vendor_data['wcfm_vacation_mode'] : 'no';
  6.     $wcfm_vacation_mode_type = isset( $vendor_data['wcfm_vacation_mode_type'] ) ? $vendor_data['wcfm_vacation_mode_type'] : 'instant';
  7.     $wcfm_vacation_start_date = isset( $vendor_data['wcfm_vacation_start_date'] ) ? $vendor_data['wcfm_vacation_start_date'] : '';
  8.     $wcfm_vacation_end_date = isset( $vendor_data['wcfm_vacation_end_date'] ) ? $vendor_data['wcfm_vacation_end_date'] : '';
  9.     if ( $vacation_mode == 'yes' ) {
  10.         if( $wcfm_vacation_mode_type == 'instant' ) {
  11.             $vacation_arg = array(
  12.                 'type' => 'days',
  13.                 'range' => array(
  14.                     '1' => false,
  15.                     '2' => false,
  16.                     '3' => false,
  17.                     '4' => false,
  18.                     '5' => false,
  19.                     '6' => false,
  20.                     '7' => false
  21.                 ),
  22.             );
  23.         } elseif( $wcfm_vacation_start_date && $wcfm_vacation_end_date ) {
  24.             $start_time = strtotime( $wcfm_vacation_start_date );
  25.             $end_time = strtotime( $wcfm_vacation_end_date );
  26.             $y = date("Y", $end_time);
  27.             $m = date("m", $end_time);
  28.             $d = date("d", $end_time);
  29.             $range = array();
  30.             while($start_time < $end_time) {
  31.                 $year = date("Y", $start_time);
  32.                 if(!isset($range[$year])) {
  33.                     $range[$year] = array();
  34.                 }
  35.                 $mon = date("n", $start_time);
  36.                 if(!isset($range[$year][$mon])) {
  37.                     $range[$year][$mon] = array();
  38.                 }
  39.                 $date = date("j", $start_time);
  40.                 if($y > $year || $m > $mon) {
  41.                     $month_days = date("t", $start_time);
  42.                     $range[$year][$mon] = array_fill($date, $month_days - $date + 1, false);
  43.                     $start_time = strtotime("+1 day", strtotime("{$year}-{$mon}-{$month_days}"));
  44.                 } else {
  45.                     $range[$year][$mon] = array_fill($date+0, $d - $date + 1, '');
  46.                     $start_time = $end_time;
  47.                 }
  48.             }
  49.             $vacation_arg = array(
  50.                 'type' => 'custom',
  51.                 'range' => $range,
  52.             );
  53.         }
  54.         if(isset($args['availability_rules'])) {
  55.             $args['availability_rules'][] = array_merge(
  56.                 $vacation_arg,
  57.                 array(
  58.                     'priority' => 1,
  59.                     'qty' => 0,
  60.                     'level' => 'product',
  61.                     'order' => 0,
  62.                     'kind_id' => $product->get_id(),
  63.                 ),
  64.             );
  65.         }
  66.     }
  67.     return $args;
  68. }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement