Search code examples
wordpresscalendarwoocommerceicalendar

Online ical feed won't work


Im working on a ical feed for that displays my woocommerce bookings but can't seem to get it to work. Could anyone please check my code for errors?

I tried http://icalendar.org/validator.html and it gives the following error:

Errors Lines not delimited by CRLF sequence near line # 1 Reference: RFC 5545 3.1. Content Lines

<?php

// - start collecting output -
ob_start();

// - file header -
header('Content-type: text/calendar');
header('Content-Disposition: attachment; filename="ical.ics"');
?>
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//bobbin v0.1//NONSGML iCal Writer//EN
CALSCALE:GREGORIAN
METHOD:PUBLISH
<?php
global $wpdb, $woocommerce, $post, $feed;

$orders = $feed->get_bookings();

foreach ($orders as $o):

    $order = new WC_Order($o->ID);
    $items = $order->get_items();
    foreach ($items as $id => $item) {
        $length = $item['item_meta']["Aantal uur (&euro;25,00)"][0];
        $item['item_meta'] = $wpdb->get_results('SELECT * FROM wp_woocommerce_order_itemmeta WHERE order_item_id='.$id, 'ARRAY_A');
        foreach ($item['item_meta'] as $meta) {
            $item['item_meta'][$meta['meta_key']] = $meta['meta_value'];
        }

        if (isset($item['item_meta']['Verhuur datum'])) {



        // var_dump($item['item_meta']);

            $date = $item['item_meta']['Verhuur datum'];
            $start = $item['item_meta']['Booking Time'];
            $length = $item['item_meta']["Aantal uur (&euro;25,00)"];
            $starttime = date('Ymd\THis', strtotime($date . " " . $start));
            $endtime = date('Ymd\THis', strtotime($date . " " . $start) + $length * 3600);

// - content header -
?>
BEGIN:VEVENT
DTSTART:<?php echo $starttime . "\n\r"; ?>
DTEND:<?php echo $endtime . "\n\r"; ?>
DTSTAMP:<?php echo date('Ymd\THis', strtotime($o->post_date)) . "\n\r"; ?>
UID:<?php echo $o->ID; ?>@se-haaglanden.nl
CREATED:<?php echo date('Ymd\THis', strtotime($o->post_date)) . "\n\r"; ?>
DESCRIPTION:<?php echo $o->post_title . "\n\r"; ?>
LAST-MODIFIED:<?php date('Ymd\THis', strtotime($o->post_modified)) . "\n\r"; ?>
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:<?php echo $o->post_title . "\n\r"; ?>
TRANSP:OPAQUE
END:VEVENT
<?php } } endforeach; ?>
END:VCALENDAR
<?php exit; ?>

Solution

  • Your editor is not ending a line with CRLF, but rather a simple LF.

    Add this code <?php echo "\r\n" ?> to each buffer line in your script.

    Do it like this:

    BEGIN:VCALENDAR<?php echo "\r\n" ?>
    VERSION:2.0<?php echo "\r\n" ?>