Export YITH Booking CSV

User
edit.php?post_type=yith_booking
Give ooption to
Export Booking in Admin

Admin can filter booking data based on From Date & To Date
Booking data will be exported in CSV format with payment status: not paid, paid, partial paid, the amount paid and remaining.

Yith premium booking plugin

Assistant
To add a **custom export feature to the YITH Booking Premium plugin** under `edit.php?post_type=yith_booking`, follow these steps:

### ✅ Goal
– **Location**: Booking List Table (Admin – `edit.php?post_type=yith_booking`)
– **Feature**: Export button + From/To date filter
– **Format**: CSV
– **Data**: Booking ID, Customer, Product, Date, Payment status, Amount paid, Remaining

## ✅ Step-by-Step Implementation

### 1. **Add Filter Fields and Export Button to Admin List Table**
Place this code in your theme’s `functions.php` or a custom plugin:

add_action('restrict_manage_posts', 'custom_booking_export_filters');
function custom_booking_export_filters() {
    global $typenow;

    if ($typenow === 'yith_booking') {
        ?>
        <input type="date" name="booking_from_date" value="<?php echo esc_attr($_GET['booking_from_date'] ?? ''); ?>" />
        <input type="date" name="booking_to_date" value="<?php echo esc_attr($_GET['booking_to_date'] ?? ''); ?>" />
        <input type="submit" class="button" name="export_booking_csv" value="Export Booking CSV" />
        <?php
    }
}

### 2. **Handle Export Logic**

add_action('admin_init', 'handle_booking_csv_export');
function handle_booking_csv_export() {
    if (!is_admin() || !current_user_can('manage_woocommerce')) return;

    if (isset($_GET['post_type'], $_GET['export_booking_csv']) && $_GET['post_type'] === 'yith_booking') {
        $from_date = sanitize_text_field($_GET['booking_from_date'] ?? '');
        $to_date   = sanitize_text_field($_GET['booking_to_date'] ?? '');

        $args = [
            'post_type'      => 'yith_booking',
            'posts_per_page' => -1,
            'post_status'    => ['publish', 'pending', 'draft'],
            'date_query'     => []
        ];

        if ($from_date) {
            $args['date_query'][] = ['after' => $from_date];
        }
        if ($to_date) {
            $args['date_query'][] = ['before' => $to_date . ' 23:59:59'];
        }

        $bookings = get_posts($args);

        header('Content-Type: text/csv');
        header('Content-Disposition: attachment;filename=booking-export.csv');

        $output = fopen('php://output', 'w');
        fputcsv($output, ['Booking ID', 'Customer', 'Product', 'Date', 'Payment Status', 'Amount Paid', 'Remaining']);

        foreach ($bookings as $booking) {
            $booking_id = $booking->ID;

            // Get product
            $product_id = get_post_meta($booking_id, '_booking_product_id', true);
            $product_title = get_the_title($product_id);

            // Customer name
            $customer_id = get_post_meta($booking_id, '_booking_customer_id', true);
            $customer = $customer_id ? get_userdata($customer_id)->display_name : 'Guest';

            // Dates
            $date = get_the_date('Y-m-d', $booking_id);

            // Payment info
            $order_id = get_post_meta($booking_id, '_booking_order_id', true);
            $paid     = 0;
            $total    = 0;
            $status   = 'Not Paid';

            if ($order_id) {
                $order = wc_get_order($order_id);
                if ($order) {
                    $total = $order->get_total();
                    $paid = $order->get_total() - $order->get_remaining_balance();
                    $remaining = $order->get_remaining_balance();

                    if ($remaining == 0) {
                        $status = 'Paid';
                    } elseif ($paid > 0) {
                        $status = 'Partial Paid';
                    }
                }
            }

            fputcsv($output, [
                $booking_id,
                $customer,
                $product_title,
                $date,
                $status,
                wc_price($paid),
                wc_price($total - $paid),
            ]);
        }

        fclose($output);
        exit;
    }
}

### ✅ Notes:
– You may need to adapt payment logic based on your actual YITH Booking + WooCommerce order data.
– You can also enhance it to use AJAX or include booking time, persons, etc.

### ✅ Optional Styling (UX)
If you want to reposition the export section above the table with better UX, use `add_action('manage_edit-yith_booking_top')`.

Let me know if you want to export **per person/room**, include **custom booking fields**, or show results **on screen before export**.

Leave a Comment

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