ICS not working for other calendars (only works for gCal)

Issue #970 new
Valentina R created an issue

The code on lines 218-219 need to be fixed to this:

$ics_a['dtstart']      = 'DTSTART;TZID=' . wc_appointment_get_timezone_string()  . $date_prefix . $this->format_date( $appointment->get_start(), $appointment );
$ics_a['dtend']        = 'DTEND;TZID=' . wc_appointment_get_timezone_string() . $date_prefix . $this->format_date( $appointment->get_end(), $appointment );

Workaround for those with this issue is to use this code snippet:

add_filter('wc_appointments_ics_appointment', function($ics_a, $appointment, $object){

    $date_prefix    = $appointment->is_all_day() ? ';VALUE=DATE:' : ':';

    $ics_a['dtstart']      = 'DTSTART;TZID=' . wc_appointment_get_timezone_string()  . $date_prefix . custom_format_date( $appointment->get_start(), $appointment );
    $ics_a['dtend']        = 'DTEND;TZID=' . wc_appointment_get_timezone_string() . $date_prefix . custom_format_date( $appointment->get_end(), $appointment );

  return $ics_a;
}, 10, 3);

function custom_format_date( $timestamp, $appointment = null ) {
        #$pattern = 'Ymd\THis\Z';
        $pattern = 'Ymd\THis';
        $old_ts  = $timestamp;

        if ( $appointment ) {
            $pattern = ( $appointment->is_all_day() ) ? 'Ymd' : $pattern;

            // If we're working on the end timestamp
            if ( $appointment->get_end() === $timestamp ) {
                // If appointments are more than 1 day, ics format for the end date should be the day after the appointment ends
                if ( strtotime( 'midnight', $appointment->get_start() ) !== strtotime( 'midnight', $appointment->get_end() ) ) {
                    $timestamp += 86400;
                }
            }
        }

        return apply_filters( 'woocommerce_appointments_ics_format_date', date( $pattern, $timestamp ), $timestamp, $old_ts, $appointment );
    }

// https://bookingwp.com/forums/topic/ics-file-not-are-not-synchronizing-correctly/

Reported here.

Comments (2)

  1. Valentina R reporter

    The workaround is not working fine for all calendar apps. Perhaps we should use this time format instead (haven’t tested it yet):

    DTSTART:20220626T220000Z
    DTEND:20220626T230000Z

    Another report here.

  2. Log in to comment