Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_global_delivery_time_format() {
- $display_format = get_global_delivery_display_format();
- $format = wc_date_format() . ' ' . wc_time_format();
- if($display_format=='date') {
- $format = wc_date_format();
- } elseif($display_format=='time') {
- $format = wc_time_format();
- }
- return $format;
- }
- function get_global_delivery_display_format() {
- if( apply_filters( 'wcfm_is_allow_delivery_time', true ) ) {
- $delivery_time_settings = get_option( 'wcfm_delivery_time_options', array() );
- $display_format = isset( $delivery_time_settings['display_format'] ) ? $delivery_time_settings['display_format'] : 'date_time';
- if(in_array($display_format, array('date', 'time', 'date_time'))) {
- return $display_format;
- }
- return 'date_time';
- }
- }
- add_filter( 'woocommerce_checkout_fields', function($fields) {
- global $WCFMd;
- remove_filter( 'woocommerce_checkout_fields', array( $WCFMd->wcfmd_delivery_time, 'wcfmd_checkout_delivery_time_field' ) );
- if( ( true === WC()->cart->needs_shipping() ) && apply_filters( 'wcfmmp_is_allow_checkout_delivery_time', true ) ) {
- $wcfm_store_hours = get_option( 'wcfm_store_hours_options', array() );
- $wcfm_delivery_time = get_option( 'wcfm_delivery_time_options', array() );
- if( !$wcfm_delivery_time ) $wcfm_delivery_time = $wcfm_store_hours;
- $hide_on_local_pickup = isset($wcfm_delivery_time['hide_field']) ? $wcfm_delivery_time['hide_field'] : 'no';
- $current_time = current_time( 'timestamp' );
- $wcfm_delivery_time_off_days = isset( $wcfm_delivery_time['off_days'] ) ? $wcfm_delivery_time['off_days'] : array();
- $off_days = count($wcfm_delivery_time_off_days);
- if( $off_days == 7 ) return $fields;
- $wcfm_delivery_time_start_from = isset( $wcfm_delivery_time['start_from'] ) ? $wcfm_delivery_time['start_from'] : 0;
- $wcfm_delivery_time_end_at = isset( $wcfm_delivery_time['end_at'] ) ? $wcfm_delivery_time['end_at'] : 0;
- $wcfm_delivery_time_slots_duration = isset( $wcfm_delivery_time['slots_duration'] ) ? $wcfm_delivery_time['slots_duration'] : 0;
- $wcfm_delivery_time_display_format = isset( $wcfm_delivery_time['display_format'] ) ? $wcfm_delivery_time['display_format'] : 'date_time';
- $wcfm_delivery_time_day_times = isset( $wcfm_delivery_time['day_times'] ) ? $wcfm_delivery_time['day_times'] : array();
- $wcfm_delivery_time_start_from_options = get_wcfm_start_from_delivery_times();
- $wcfm_delivery_time_end_at_options = get_wcfm_end_at_delivery_times();
- $wcfm_delivery_time_slots_duration_options = get_wcfm_slots_duration_delivery_times();
- $start_time = strtotime( '+' . $wcfm_delivery_time_start_from_options[$wcfm_delivery_time_start_from], $current_time );
- $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);
- $interval_in_minutes = $WCFMd->wcfmd_delivery_time->get_minutes_from_str($wcfm_delivery_time_slots_duration_options[$wcfm_delivery_time_slots_duration], $current_time);
- $time_slots = array( '' => __( 'Choose preferred delivery time', 'wc-frontend-manager-delivery' ) );
- $next_time_slot = $start_time;
- $first_time_flag = false;
- $max_iteration_with_no_change = 8; // 7days + 1 extra to fully check the starting day
- $previous_minutes_remaining = '';
- while ( $minutes_remaining_before_end > 0 && $max_iteration_with_no_change > 0 ) {
- if($previous_minutes_remaining == $minutes_remaining_before_end) {
- $max_iteration_with_no_change--;
- } else {
- $max_iteration_with_no_change = 8;
- $previous_minutes_remaining = $minutes_remaining_before_end;
- }
- $week_date = date( 'Y-m-d', $next_time_slot );
- $weekday = date( 'N', $next_time_slot ) - 1;
- if ( ! empty( $wcfm_delivery_time_off_days ) ) {
- if ( in_array( $weekday, $wcfm_delivery_time_off_days ) ) {
- $next_time_slot = strtotime( $week_date . ' +1 day' ); //go to next day 12:00am
- $first_time_flag = true;
- continue;
- }
- }
- $daywise_slot_rules_applied = false;
- $current_day_slot_available = false;
- if ( !empty( $wcfm_delivery_time_day_times[$weekday] ) ) {
- $tmp_remaining_minutes = $minutes_remaining_before_end;
- $wcfm_delivery_time_day_time_slots = $wcfm_delivery_time_day_times[$weekday];
- foreach ( $wcfm_delivery_time_day_time_slots as $slot => $wcfm_delivery_time_day_time_slot ) {
- $open_hours = !empty( $wcfm_delivery_time_day_time_slot['start'] ) ? strtotime( $week_date . ' ' . $wcfm_delivery_time_day_time_slot['start'] ) : 0;
- $close_hours = !empty( $wcfm_delivery_time_day_time_slot['end'] ) ? strtotime( $week_date . ' ' . $wcfm_delivery_time_day_time_slot['end'] ) : 0;
- if(!$open_hours || !$close_hours || $open_hours >= $close_hours ) {
- continue;
- }
- $current_day_slot_available = true;
- while( $close_hours>$next_time_slot && $minutes_remaining_before_end > 0 ) {
- if($open_hours>=$next_time_slot || $interval_in_minutes >= 1440) { // 1day = 1440 min
- $next_time_slot = $open_hours;
- } else {
- $time_diff_in_minute = floor(($next_time_slot - $open_hours)/60);
- $minutes_to_be_adjusted = $time_diff_in_minute % $interval_in_minutes;
- $next_time_slot = strtotime( "-{$minutes_to_be_adjusted} minutes", $next_time_slot );
- //only for the 1st time
- if(!$first_time_flag && $minutes_to_be_adjusted && apply_filters('wcfm_delivery_time_restrict_getting_current_running_slot', true)) {
- $next_time_slot = strtotime( "+{$interval_in_minutes} minutes", $next_time_slot ); //can't use a slot if it already started.
- if($next_time_slot >= $close_hours) {
- $first_time_flag = true;
- continue;
- }
- }
- }
- $option_val = $WCFMd->wcfmd_delivery_time->prepare_delivery_time_range( $wcfm_delivery_time_display_format, $next_time_slot, $interval_in_minutes, $close_hours );
- $time_slots[$option_val] = $WCFMd->wcfmd_delivery_time->format_delivery_time_checkout_field($wcfm_delivery_time_display_format, $option_val);
- $next_time_slot = strtotime( "+{$interval_in_minutes} minutes", $next_time_slot );
- $minutes_remaining_before_end -= $interval_in_minutes;
- if($next_time_slot>$close_hours) {
- //adjust remining time
- $minutes_remaining_before_end += floor(($next_time_slot-$close_hours)/60);
- $next_time_slot=$close_hours;
- }
- $daywise_slot_rules_applied = true;
- if(!$first_time_flag) {
- $first_time_flag = true;
- }
- }
- }
- }
- if($daywise_slot_rules_applied) {
- $next_time_slot = strtotime( $week_date . ' +1 day' ); //go to next day 12:00am
- $minutes_remaining_before_end = $tmp_remaining_minutes - 1440; //1 day = 1440 minutes
- } elseif($current_day_slot_available) {
- $next_time_slot = strtotime( $week_date . ' +1 day' ); //go to next day 12:00am
- }else {
- if(apply_filters('wcfm_roundoff_delivery_time_checkout_field', true) && $wcfm_delivery_time_display_format !== 'date') {
- $minute = (int) date('i', $next_time_slot);
- $first_time_adjustment = $interval_in_minutes;
- if($interval_in_minutes < 60) {
- $minute_adjustment = ($minute % $interval_in_minutes);
- $next_time_slot = strtotime("-{$minute_adjustment} minutes", $next_time_slot );
- } else {
- $next_time_slot = strtotime("-{$minute} minutes", $next_time_slot );
- $first_time_adjustment = 60;
- }
- //only for the 1st time
- if(!$first_time_flag && apply_filters('wcfm_delivery_time_restrict_getting_current_running_slot', true)) {
- $next_time_slot = strtotime( "+{$first_time_adjustment} minutes", $next_time_slot ); //can't use a slot if it already started.
- }
- }
- if(!$first_time_flag) {
- $first_time_flag = true;
- }
- $option_val = $WCFMd->wcfmd_delivery_time->prepare_delivery_time_range( $wcfm_delivery_time_display_format, $next_time_slot, $interval_in_minutes );
- $time_slots[$option_val] = $WCFMd->wcfmd_delivery_time->format_delivery_time_checkout_field($wcfm_delivery_time_display_format, $option_val);
- $next_time_slot = strtotime( '+' . $wcfm_delivery_time_slots_duration_options[$wcfm_delivery_time_slots_duration], $next_time_slot );
- $minutes_remaining_before_end -= $interval_in_minutes;
- }
- }
- if(count($time_slots) == 1 ) return $fields; //no change
- $field_id = 'wcfmd_delivery_time_global';
- $field_label = __( 'Delivery Time', 'wc-frontend-manager-delivery' );
- $delivery_time_field = array(
- $field_id => array(
- 'label' => $field_label,
- 'type' => 'select',
- 'required' => true,
- 'options' => $time_slots,
- 'class' => array( 'form-row-wide' ),
- 'priority' => 1,
- 'clear' => true,
- ),
- );
- if($hide_on_local_pickup === 'yes') {
- $delivery_time_field[$field_id]['required'] = false;
- $delivery_time_field += array(
- "_{$field_id}_is_required" => array(
- 'class' => array('wcfm_custom_hide'),
- 'value' => 'yes',
- )
- );
- }
- $position = apply_filters('wcfmd_delivery_time_field_position', 'billing');
- if( !in_array($position, array('billing', 'shipping'))) {
- $position = 'billing';
- }
- $fields[$position] = $delivery_time_field + $fields[$position];
- return $fields;
- }
- }, 9 );
- add_action( 'woocommerce_checkout_update_order_meta', function( $order_id, $data ) {
- global $WCFMd;
- remove_action( 'woocommerce_checkout_update_order_meta', array( $WCFMd->wcfmd_delivery_time, 'wcfmd_checkout_delivery_time_field_save' ), 50 );
- $order = wc_get_order( $order_id );
- if( !is_a( $order , 'WC_Order' ) ) return;
- $order_delivery_time = '';
- if ( isset( $data['wcfmd_delivery_time_global'] ) && ! empty( $data['wcfmd_delivery_time_global'] ) ) {
- $order_delivery_time = $data['wcfmd_delivery_time_global'];
- }
- if ( ! empty( $order_delivery_time ) ) {
- update_post_meta( $order_id, '_wcfmd_delivery_time_global', $order_delivery_time );
- }
- }, 49, 2 );
- add_action( 'wcfm_order_details_after_address', function($order) {
- global $WCFMd;
- remove_action( 'wcfm_order_details_after_address', array( $WCFMd->wcfmd_delivery_time, 'wcfmd_order_details_delivery_time_show' ), 60 );
- if( apply_filters( 'wcfm_is_allow_delivery_time', true ) ) {
- $wcfmd_delivery_time = get_post_meta( $order->get_id(), '_wcfmd_delivery_time_global', true );
- echo '<p class="wcfm_order_details_delivery_time"><i class="wcfmfa fa-clock" style="color:#ff1400"></i> <strong>';
- $time_format = get_global_delivery_time_format();
- echo __( 'Delivery Time', 'wc-frontend-manager-delivery' ).':</strong> ' . $WCFMd->wcfmd_delivery_time->format_delivery_time_checkout_field($time_format, $wcfmd_delivery_time ) . '</p>';
- }
- }, 59 );
- add_filter( 'wcfm_orderlist_shipping_address', function( $shipping_address, $order_id ) {
- global $WCFMd;
- remove_action( 'wcfm_orderlist_shipping_address', array( $WCFMd->wcfmd_delivery_time, 'wcfmd_order_list_delivery_time_show' ), 60 );
- if( apply_filters( 'wcfm_is_allow_delivery_time', true ) ) {
- $wcfmd_delvery_time = get_post_meta( $order_id, '_wcfmd_delivery_time_global', true );
- if( !empty( $wcfmd_delvery_time ) ) {
- $shipping_address .= '<br/><p class="wcfm_order_list_delivery_time"><i class="wcfmfa fa-clock" style="color:#ff1400"></i> <strong>';
- $time_format = get_global_delivery_time_format();
- $shipping_address .= __( 'Delivery Time', 'wc-frontend-manager-delivery' ).':</strong> ' . $WCFMd->wcfmd_delivery_time->format_delivery_time_checkout_field($time_format, $wcfmd_delvery_time ) . '</p>';
- }
- }
- return $shipping_address;
- }, 59, 2 );
- function wcfmd_customer_order_delivery_time_show( $order, $email ) {
- global $WCFMd;
- if( function_exists('is_wcfm_page') && is_wcfm_page() ) return;
- 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;
- $wcfmd_delivery_time = get_post_meta( $order->get_id(), '_wcfmd_delivery_time_global', true );
- if( empty( $wcfmd_delivery_time ) ) return;
- echo "<br />";
- echo "<table width='100%' style='width:100%;'><tbody>";
- $display_format = get_global_delivery_display_format();
- ?>
- <tr>
- <td colspan="3" style="background-color: #eeeeee;padding: 1em 1.41575em;line-height: 1.5;font-weight:600;">
- <?php
- echo __( 'Delivery Time(s)', 'wc-frontend-manager-delivery' );
- ?>
- </td>
- <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>
- </tr>
- <?php
- echo "</tbody></table>";
- echo "<br />";
- }
- add_action( 'woocommerce_order_details_after_order_table', function($order, $is_plain = 0, $is_admin = 0, $email = false, $preferred_vendor = 0) {
- global $WCFMd;
- remove_action( 'woocommerce_order_details_after_order_table', array( $WCFMd->wcfmd_delivery_time, 'wcfmd_customer_order_delivery_time_show' ), 12 );
- wcfmd_customer_order_delivery_time_show($order, $email);
- }, 11, 5 );
- add_action( 'woocommerce_email_order_meta', function($order, $is_plain = 0, $is_admin = 0, $email = false, $preferred_vendor = 0) {
- global $WCFMd;
- remove_action( 'woocommerce_email_order_meta', array( $WCFMd->wcfmd_delivery_time, 'wcfmd_customer_order_delivery_time_show' ), 12 );
- if($email && $email->id !== 'store-new-order') {
- wcfmd_customer_order_delivery_time_show($order, $email);
- }
- }, 11, 5 );
- add_action( 'wcfm_after_store_invoice_order_details', function($vendor_id, $order_id, $order) {
- global $WCFMd;
- remove_action( 'wcfm_after_store_invoice_order_details', array( $WCFMd->wcfmd_delivery_time, 'wcfmd_store_invoice_delivery_time_show' ), 12 );
- wcfmd_customer_order_delivery_time_show($order, false);
- }, 11, 3 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement