Advertisement
palsushobhan

checkout-global-admin-delivery-time.php

Jun 7th, 2021
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.37 KB | None | 0 0
  1. function get_global_delivery_time_format() {
  2.     $display_format = get_global_delivery_display_format();
  3.     $format = wc_date_format() . ' ' . wc_time_format();
  4.     if($display_format=='date') {
  5.         $format = wc_date_format();
  6.     } elseif($display_format=='time') {
  7.         $format = wc_time_format();
  8.     }
  9.     return $format;
  10. }
  11.  
  12. function get_global_delivery_display_format() {
  13.     if( apply_filters( 'wcfm_is_allow_delivery_time', true ) ) {
  14.         $delivery_time_settings = get_option( 'wcfm_delivery_time_options', array() );
  15.         $display_format = isset( $delivery_time_settings['display_format'] ) ? $delivery_time_settings['display_format'] : 'date_time';
  16.         if(in_array($display_format, array('date', 'time', 'date_time'))) {
  17.             return $display_format;
  18.         }
  19.         return 'date_time';
  20.     }
  21. }
  22.  
  23. add_filter( 'woocommerce_checkout_fields', function($fields) {
  24.     global $WCFMd;
  25.    
  26.     remove_filter( 'woocommerce_checkout_fields', array( $WCFMd->wcfmd_delivery_time, 'wcfmd_checkout_delivery_time_field' ) );
  27.  
  28.     if( ( true === WC()->cart->needs_shipping() ) && apply_filters( 'wcfmmp_is_allow_checkout_delivery_time', true ) ) {
  29.         $wcfm_store_hours = get_option( 'wcfm_store_hours_options', array() );
  30.         $wcfm_delivery_time = get_option( 'wcfm_delivery_time_options', array() );
  31.         if( !$wcfm_delivery_time ) $wcfm_delivery_time = $wcfm_store_hours;
  32.  
  33.         $hide_on_local_pickup = isset($wcfm_delivery_time['hide_field']) ? $wcfm_delivery_time['hide_field'] : 'no';
  34.         $current_time = current_time( 'timestamp' );
  35.         $wcfm_delivery_time_off_days = isset( $wcfm_delivery_time['off_days'] ) ? $wcfm_delivery_time['off_days'] : array();
  36.        
  37.         $off_days = count($wcfm_delivery_time_off_days);
  38.         if( $off_days == 7 ) return $fields;
  39.        
  40.         $wcfm_delivery_time_start_from = isset( $wcfm_delivery_time['start_from'] ) ? $wcfm_delivery_time['start_from'] : 0;
  41.         $wcfm_delivery_time_end_at = isset( $wcfm_delivery_time['end_at'] ) ? $wcfm_delivery_time['end_at'] : 0;
  42.         $wcfm_delivery_time_slots_duration = isset( $wcfm_delivery_time['slots_duration'] ) ? $wcfm_delivery_time['slots_duration'] : 0;
  43.         $wcfm_delivery_time_display_format = isset( $wcfm_delivery_time['display_format'] ) ? $wcfm_delivery_time['display_format'] : 'date_time';
  44.  
  45.         $wcfm_delivery_time_day_times = isset( $wcfm_delivery_time['day_times'] ) ? $wcfm_delivery_time['day_times'] : array();
  46.  
  47.         $wcfm_delivery_time_start_from_options = get_wcfm_start_from_delivery_times();
  48.         $wcfm_delivery_time_end_at_options = get_wcfm_end_at_delivery_times();
  49.         $wcfm_delivery_time_slots_duration_options = get_wcfm_slots_duration_delivery_times();
  50.  
  51.         $start_time = strtotime( '+' . $wcfm_delivery_time_start_from_options[$wcfm_delivery_time_start_from], $current_time );
  52.         $minutes_remaining_before_end = $WCFMd->wcfmd_delivery_time->get_minutes_from_str($wcfm_delivery_time_end_at_options[$wcfm_delivery_time_end_at], $current_time);
  53.         $interval_in_minutes = $WCFMd->wcfmd_delivery_time->get_minutes_from_str($wcfm_delivery_time_slots_duration_options[$wcfm_delivery_time_slots_duration], $current_time);
  54.        
  55.         $time_slots = array( '' => __( 'Choose preferred delivery time', 'wc-frontend-manager-delivery' ) );
  56.         $next_time_slot = $start_time;
  57.         $first_time_flag = false;
  58.         $max_iteration_with_no_change = 8; // 7days + 1 extra to fully check the starting day
  59.         $previous_minutes_remaining = '';
  60.         while ( $minutes_remaining_before_end > 0 && $max_iteration_with_no_change > 0 ) {
  61.             if($previous_minutes_remaining == $minutes_remaining_before_end) {
  62.                 $max_iteration_with_no_change--;
  63.             } else {
  64.                 $max_iteration_with_no_change = 8;
  65.                 $previous_minutes_remaining = $minutes_remaining_before_end;
  66.             }
  67.             $week_date = date( 'Y-m-d', $next_time_slot );
  68.             $weekday = date( 'N', $next_time_slot ) - 1;
  69.             if ( ! empty( $wcfm_delivery_time_off_days ) ) {
  70.                 if ( in_array( $weekday, $wcfm_delivery_time_off_days ) ) {
  71.                     $next_time_slot = strtotime( $week_date . ' +1 day' ); //go to next day 12:00am
  72.                     $first_time_flag = true;
  73.                     continue;
  74.                 }
  75.             }
  76.            
  77.             $daywise_slot_rules_applied = false;
  78.             $current_day_slot_available = false;
  79.             if ( !empty( $wcfm_delivery_time_day_times[$weekday] ) ) {
  80.                 $tmp_remaining_minutes = $minutes_remaining_before_end;
  81.                 $wcfm_delivery_time_day_time_slots = $wcfm_delivery_time_day_times[$weekday];
  82.                 foreach ( $wcfm_delivery_time_day_time_slots as $slot => $wcfm_delivery_time_day_time_slot ) {
  83.                     $open_hours = !empty( $wcfm_delivery_time_day_time_slot['start'] ) ? strtotime( $week_date . ' ' . $wcfm_delivery_time_day_time_slot['start'] ) : 0;
  84.                     $close_hours = !empty( $wcfm_delivery_time_day_time_slot['end'] ) ? strtotime( $week_date . ' ' . $wcfm_delivery_time_day_time_slot['end'] ) : 0;
  85.                     if(!$open_hours || !$close_hours || $open_hours >= $close_hours ) {
  86.                         continue;
  87.                     }
  88.                     $current_day_slot_available = true;
  89.  
  90.                     while( $close_hours>$next_time_slot && $minutes_remaining_before_end > 0 ) {
  91.                         if($open_hours>=$next_time_slot || $interval_in_minutes >= 1440) { // 1day = 1440 min
  92.                             $next_time_slot = $open_hours;
  93.                         } else {
  94.                             $time_diff_in_minute = floor(($next_time_slot - $open_hours)/60);
  95.                             $minutes_to_be_adjusted = $time_diff_in_minute % $interval_in_minutes;
  96.                             $next_time_slot = strtotime( "-{$minutes_to_be_adjusted} minutes", $next_time_slot );
  97.                            
  98.                             //only for the 1st time
  99.                             if(!$first_time_flag && $minutes_to_be_adjusted && apply_filters('wcfm_delivery_time_restrict_getting_current_running_slot', true)) {
  100.                                 $next_time_slot = strtotime( "+{$interval_in_minutes} minutes", $next_time_slot ); //can't use a slot if it already started.
  101.                                 if($next_time_slot >= $close_hours) {
  102.                                     $first_time_flag = true;
  103.                                     continue;
  104.                                 }
  105.                             }
  106.                         }
  107.                         $option_val = $WCFMd->wcfmd_delivery_time->prepare_delivery_time_range( $wcfm_delivery_time_display_format, $next_time_slot, $interval_in_minutes, $close_hours );
  108.                         $time_slots[$option_val] = $WCFMd->wcfmd_delivery_time->format_delivery_time_checkout_field($wcfm_delivery_time_display_format, $option_val);
  109.                         $next_time_slot = strtotime( "+{$interval_in_minutes} minutes", $next_time_slot );
  110.                         $minutes_remaining_before_end -= $interval_in_minutes;
  111.                        
  112.                         if($next_time_slot>$close_hours) {
  113.                             //adjust remining time
  114.                             $minutes_remaining_before_end += floor(($next_time_slot-$close_hours)/60);
  115.                             $next_time_slot=$close_hours;
  116.                            
  117.                         }
  118.                         $daywise_slot_rules_applied = true;
  119.                         if(!$first_time_flag) {
  120.                             $first_time_flag = true;
  121.                         }
  122.                     }
  123.                 }
  124.             }
  125.            
  126.             if($daywise_slot_rules_applied) {
  127.                 $next_time_slot = strtotime( $week_date . ' +1 day' ); //go to next day 12:00am
  128.                 $minutes_remaining_before_end = $tmp_remaining_minutes - 1440; //1 day = 1440 minutes
  129.             } elseif($current_day_slot_available) {
  130.                 $next_time_slot = strtotime( $week_date . ' +1 day' ); //go to next day 12:00am
  131.             }else {
  132.                 if(apply_filters('wcfm_roundoff_delivery_time_checkout_field', true) && $wcfm_delivery_time_display_format !== 'date') {
  133.                     $minute = (int) date('i', $next_time_slot);
  134.                     $first_time_adjustment = $interval_in_minutes;
  135.                     if($interval_in_minutes < 60) {
  136.                         $minute_adjustment = ($minute % $interval_in_minutes);
  137.                         $next_time_slot = strtotime("-{$minute_adjustment} minutes", $next_time_slot );
  138.                     } else {
  139.                         $next_time_slot = strtotime("-{$minute} minutes", $next_time_slot );
  140.                         $first_time_adjustment = 60;
  141.                     }
  142.                     //only for the 1st time
  143.                     if(!$first_time_flag && apply_filters('wcfm_delivery_time_restrict_getting_current_running_slot', true)) {
  144.                         $next_time_slot = strtotime( "+{$first_time_adjustment} minutes", $next_time_slot ); //can't use a slot if it already started.
  145.                     }
  146.                 }
  147.                 if(!$first_time_flag) {
  148.                     $first_time_flag = true;
  149.                 }
  150.                 $option_val = $WCFMd->wcfmd_delivery_time->prepare_delivery_time_range( $wcfm_delivery_time_display_format, $next_time_slot, $interval_in_minutes );
  151.                 $time_slots[$option_val] = $WCFMd->wcfmd_delivery_time->format_delivery_time_checkout_field($wcfm_delivery_time_display_format, $option_val);
  152.  
  153.                 $next_time_slot = strtotime( '+' . $wcfm_delivery_time_slots_duration_options[$wcfm_delivery_time_slots_duration], $next_time_slot );
  154.                 $minutes_remaining_before_end -= $interval_in_minutes;
  155.             }
  156.         }
  157.        
  158.         if(count($time_slots) == 1 ) return $fields; //no change
  159.  
  160.         $field_id = 'wcfmd_delivery_time_global';
  161.  
  162.         $field_label = __( 'Delivery Time', 'wc-frontend-manager-delivery' );
  163.  
  164.         $delivery_time_field = array(
  165.             $field_id => array(
  166.                 'label'    => $field_label,
  167.                 'type'     => 'select',
  168.                 'required' => true,
  169.                 'options'  => $time_slots,
  170.                 'class'    => array( 'form-row-wide' ),
  171.                 'priority' => 1,
  172.                 'clear'    => true,
  173.             ),
  174.          );
  175.          if($hide_on_local_pickup === 'yes') {
  176.             $delivery_time_field[$field_id]['required'] = false;
  177.             $delivery_time_field += array(
  178.                 "_{$field_id}_is_required" => array(
  179.                     'class'     => array('wcfm_custom_hide'),
  180.                     'value'     => 'yes',
  181.                 )
  182.             );
  183.          }
  184.            
  185.         $position = apply_filters('wcfmd_delivery_time_field_position', 'billing');
  186.         if( !in_array($position, array('billing', 'shipping'))) {
  187.             $position = 'billing';
  188.         }
  189.         $fields[$position] = $delivery_time_field + $fields[$position];
  190.  
  191.         return $fields;
  192.     }
  193. }, 9 );
  194.  
  195. add_action( 'woocommerce_checkout_update_order_meta', function( $order_id, $data ) {
  196.     global $WCFMd;
  197.     remove_action( 'woocommerce_checkout_update_order_meta', array( $WCFMd->wcfmd_delivery_time, 'wcfmd_checkout_delivery_time_field_save' ), 50 );
  198.  
  199.     $order = wc_get_order( $order_id );
  200.     if( !is_a( $order , 'WC_Order' ) ) return;
  201.    
  202.     $order_delivery_time = '';
  203.     if ( isset( $data['wcfmd_delivery_time_global'] ) && ! empty( $data['wcfmd_delivery_time_global'] ) ) {
  204.         $order_delivery_time = $data['wcfmd_delivery_time_global'];
  205.     }
  206.  
  207.     if ( ! empty( $order_delivery_time ) ) {
  208.         update_post_meta( $order_id, '_wcfmd_delivery_time_global', $order_delivery_time );
  209.     }
  210. }, 49, 2 );
  211.  
  212. add_action( 'wcfm_order_details_after_address', function($order) {
  213.     global $WCFMd;
  214.     remove_action( 'wcfm_order_details_after_address', array( $WCFMd->wcfmd_delivery_time, 'wcfmd_order_details_delivery_time_show' ), 60 );
  215.  
  216.     if( apply_filters( 'wcfm_is_allow_delivery_time', true ) ) {
  217.         $wcfmd_delivery_time = get_post_meta( $order->get_id(), '_wcfmd_delivery_time_global', true );
  218.         echo '<p class="wcfm_order_details_delivery_time"><i class="wcfmfa fa-clock" style="color:#ff1400"></i>&nbsp;&nbsp;<strong>';
  219.         $time_format = get_global_delivery_time_format();
  220.         echo __( 'Delivery Time', 'wc-frontend-manager-delivery' ).':</strong> ' . $WCFMd->wcfmd_delivery_time->format_delivery_time_checkout_field($time_format, $wcfmd_delivery_time ) . '</p>';
  221.     }
  222. }, 59 );
  223.  
  224. add_filter( 'wcfm_orderlist_shipping_address', function( $shipping_address, $order_id ) {
  225.     global $WCFMd;
  226.     remove_action( 'wcfm_orderlist_shipping_address', array( $WCFMd->wcfmd_delivery_time, 'wcfmd_order_list_delivery_time_show' ), 60 );
  227.  
  228.     if( apply_filters( 'wcfm_is_allow_delivery_time', true ) ) {
  229.         $wcfmd_delvery_time = get_post_meta( $order_id, '_wcfmd_delivery_time_global', true );
  230.         if( !empty(  $wcfmd_delvery_time ) ) {
  231.             $shipping_address .=  '<br/><p class="wcfm_order_list_delivery_time"><i class="wcfmfa fa-clock" style="color:#ff1400"></i>&nbsp;&nbsp;<strong>';
  232.             $time_format = get_global_delivery_time_format();
  233.             $shipping_address .= __( 'Delivery Time', 'wc-frontend-manager-delivery' ).':</strong> ' . $WCFMd->wcfmd_delivery_time->format_delivery_time_checkout_field($time_format, $wcfmd_delvery_time ) . '</p>';
  234.         }
  235.     }
  236.     return $shipping_address;
  237. }, 59, 2 );
  238.  
  239. function wcfmd_customer_order_delivery_time_show( $order, $email ) {
  240.     global $WCFMd;
  241.  
  242.     if( function_exists('is_wcfm_page') && is_wcfm_page() ) return;
  243.     if( $email && !in_array( $email->id, array( 'new_order', 'store-new-order', 'customer_invoice', 'customer_on_hold_order', 'customer_processing_order', 'customer_completed_order' ) ) ) return;
  244.     $wcfmd_delivery_time = get_post_meta( $order->get_id(), '_wcfmd_delivery_time_global', true );
  245.     if( empty(  $wcfmd_delivery_time ) ) return;
  246.            
  247.     echo "<br />";
  248.     echo "<table width='100%' style='width:100%;'><tbody>";
  249.  
  250.     $display_format = get_global_delivery_display_format();
  251.     ?>
  252.     <tr>
  253.         <td colspan="3" style="background-color: #eeeeee;padding: 1em 1.41575em;line-height: 1.5;font-weight:600;">
  254.             <?php
  255.             echo __( 'Delivery Time(s)', 'wc-frontend-manager-delivery' );
  256.             ?>
  257.         </td>
  258.         <td colspan="5" style="background-color: #f8f8f8;padding: 1em;"><?php echo  $WCFMd->wcfmd_delivery_time->format_delivery_time_checkout_field($display_format, $wcfmd_delivery_time ); ?></td>
  259.     </tr>
  260.     <?php
  261.     echo "</tbody></table>";
  262.     echo "<br />";
  263. }
  264.  
  265. add_action( 'woocommerce_order_details_after_order_table', function($order, $is_plain = 0, $is_admin = 0, $email = false, $preferred_vendor = 0) {
  266.     global $WCFMd;
  267.     remove_action( 'woocommerce_order_details_after_order_table', array( $WCFMd->wcfmd_delivery_time, 'wcfmd_customer_order_delivery_time_show' ), 12 );
  268.     wcfmd_customer_order_delivery_time_show($order, $email);
  269. }, 11, 5  );
  270.  
  271. add_action( 'woocommerce_email_order_meta', function($order, $is_plain = 0, $is_admin = 0, $email = false, $preferred_vendor = 0) {
  272.     global $WCFMd;
  273.     remove_action( 'woocommerce_email_order_meta', array( $WCFMd->wcfmd_delivery_time, 'wcfmd_customer_order_delivery_time_show' ), 12 );
  274.     if($email && $email->id !== 'store-new-order') {
  275.         wcfmd_customer_order_delivery_time_show($order, $email);
  276.     }
  277. }, 11, 5  );
  278.  
  279. add_action( 'wcfm_after_store_invoice_order_details', function($vendor_id, $order_id, $order) {
  280.     global $WCFMd;
  281.     remove_action( 'wcfm_after_store_invoice_order_details', array( $WCFMd->wcfmd_delivery_time, 'wcfmd_store_invoice_delivery_time_show' ), 12 );
  282.     wcfmd_customer_order_delivery_time_show($order, false);
  283. }, 11, 3 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement