Export WooCommerce Bookings by booking date using Advanced Order Export For WooCommerce

Let’s say you are renting out multiple rooms, and want to export all the the rooms which are rented out on a specific date. You will need an export plugin (Advanced Order Export For WooCommerce). This plugin will let you export orders for a given date range, but we want to filter by booking date, not by order date.

I found this snippet, but this code only includes bookings which have the same start date as your requested date. Bookings for multiple days will only be included in the export for the first day.

The tweaked code will filter all bookings with a start date later or equal to the requested from-date AND an end date sooner or equal to the requested to-date.

//remap settings
add_filter( 'woe_settings_validate_defaults', function( $settings ){
    if( $settings['from_date'] ) {
        $settings['book_start'] = date( 'Ymd', strtotime( $settings['from_date'] ) ) . '000000';
        $settings['from_date'] = '';
    }
    if( $settings['to_date'] ) {
        $settings['book_end'] = date( 'Ymd', strtotime( $settings['to_date'] ) ) . '000000';
        $settings['to_date'] = '';
    }
    return $settings;
});

//filter orders
add_filter( 'woe_sql_get_order_ids_where', function( $where, $settings ){
    global $wpdb;
    $where[]= "orders.ID IN ( SELECT DISTINCT post_parent FROM {$wpdb->posts} WHERE ID IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='_booking_start' AND meta_value <= {$settings['book_start']}) AND ID IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='_booking_end' AND meta_value >= {$settings['book_end']}))";
    return $where;
}, 10, 2);

Leave a Reply

Your email address will not be published. Required fields are marked *

Overtuigd?

Neem dan contact op voor een offerte of vrijblijvend gesprek.
Stuur een e-mail naar [email protected] of maak gebruik van het contactformulier.