Skip to content
Advertisement

Display bookings date time in Woocommerce admin order list

In Woocommerce with Woocommerce Bookings, I have the following to add the booking details in WooCommerce order preview, below each order item:

function get_booking_id_from_order_item( $item_id ) {
    global $wpdb;
    return (int) $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_booking_order_item_id' AND meta_value = %d", $item_id ) );
}

add_filter('woocommerce_admin_order_preview_line_item_columns', 'filter_admin_order_preview_line_item_columns', 10, 2 );
function filter_admin_order_preview_line_item_columns( $columns, $order ) {
    $first_column = $columns['product'];
    unset($columns['product']);

    return array_merge( array('product2' => $first_column ), $columns );
}

add_filter('woocommerce_admin_order_preview_line_item_column_product2', 'filter_admin_order_preview_line_item_column_product2', 10, 4 );
function filter_admin_order_preview_line_item_column_product2( $html, $item, $item_id, $order ) {
    $product = is_callable( array( $item, 'get_product' ) ) ? $item->get_product() : null;

    $html   .= wp_kses_post( $item->get_name() );

    if ( $product ) {
        $html .= '<div class="wc-order-item-sku">' . esc_html( $product->get_sku() ) . '</div>';
    }

    if ( $booking_id = get_booking_id_from_order_item( $item_id ) ) :
    $booking      = get_wc_booking( $booking_id );
    $date_format  = wc_date_format();
    $date_format .= ' ' . wc_time_format(); // ==> Comment this line if time is not needed in bookingstart date format

    ob_start(); // Start buffering
    ?>
    <div class="wc-booking-summary">
        <strong class="wc-booking-summary-number">
        <?php printf( __( 'Booking #%d', 'woocommerce-bookings' ), $booking_id ); ?>
            <span class="status-<?php echo $booking->get_status(); ?>"><?php echo ucfirst( $booking->get_status() ); ?></span>
        </strong>
        <div class="wc-booking-summary-list">
        <li>
        <?php echo esc_html( apply_filters( 'wc_bookings_summary_list_date', date_i18n( $date_format, $booking->get_start() ), $booking->get_start(), $booking->get_end() ) );
        if ( wc_should_convert_timezone( $booking ) ) :
            /* translators: %s: timezone name */
            echo esc_html( sprintf( __( 'in timezone: %s', 'woocommerce-bookings' ), $booking->get_local_timezone() ) );
        endif;
        ?>
        </li>
        <?php if ( $resource = $booking->get_resource() ) : ?>
            <li>
            <?php
            $label = method_exists( $resource, 'get_label' ) ? $resource->get_label() : __('Resource', 'woocommerce-bookings');
            /* translators: 1: label 2: resource name */
            echo esc_html( sprintf( __( '%1$s: %2$s', 'woocommerce-bookings' ), $label, $resource->get_name() ) );
            ?>
            </li>
        <?php endif;

        if ( $product && $product->has_persons() ) {
            if ( $product->has_person_types() ) {
                $person_types  = $product->get_person_types();
                $person_counts = $booking->get_person_counts();

                if ( ! empty( $person_types ) && is_array( $person_types ) ) {
                    foreach ( $person_types as $person_type ) {

                        if ( empty( $person_counts[ $person_type->get_id() ] ) ) {
                            continue;
                        }

                        ?>
                        <li><?php echo esc_html( sprintf( '%s: %d', $person_type->get_name(), $person_counts[ $person_type->get_id() ] ) ); ?></li>
                        <?php
                    }
                }
            } else {
                ?>
                <li>
                <?php
                /* translators: 1: person count */
                echo esc_html( sprintf( __( '%d Persons', 'woocommerce-bookings' ), array_sum( $booking->get_person_counts() ) ) );
                ?>
                </li>
                <?php
            }
        }
        ?>
    </ul>
    </div>
    <?php
    $html .= ob_get_clean(); // Set back buffered content
    endif;

    $meta_data = $item->get_formatted_meta_data( '' );

    if ( $meta_data ) {
        $html .= '<table cellspacing="0" class="wc-order-item-meta">';

        foreach ( $meta_data as $meta_id => $meta ) {
            if ( in_array( $meta->key, $hidden_order_itemmeta, true ) ) {
                continue;
            }
            $html .= '<tr><th>' . wp_kses_post( $meta->display_key ) . ':</th><td>' . wp_kses_post( force_balance_tags( $meta->display_value ) ) . '</td></tr>';
        }
        $html .= '</table>';
    }
    return $html;
}

How to display the booking date and time (not the order date and time) in in the woocommerce admin orders list under the billing_address column?

Any help is appreciated.

enter image description here

Advertisement

Answer

You can use the following to display the bookings date and time in the woocommerce admin orders list under the billing_address column:

add_action( 'manage_shop_order_posts_custom_column', 'display_bookings_datetime_orders_admin_list', 20, 2 );
function display_bookings_datetime_orders_admin_list( $column, $post_id ) {
    if ( 'billing_address' === $column ) {
        global $post, $the_order;

        $order = is_a($the_order, 'WC_Order') ? $the_order : wc_get_order( $post->ID ); // Get WC_Order Object

        $date_format  = wc_date_format();
        $date_format .= ' ' . wc_time_format(); // ==> Comment this line if time is not needed in booking start date format

        $datetimes    = array(); // Initializing

        // Loop through order items
        foreach ( $order->get_items() as $item_id => $item ) {
            $product = $item->get_product();

            if ( ! $product->is_type('booking') ) {
                continue;
            }
            $booking_ids = WC_Booking_Data_Store::get_booking_ids_from_order_item_id( $item_id );

            // Loop through booking Ids in the order
            foreach ( $booking_ids as $booking_id ) {
                $booking = get_wc_booking( $booking_id );

                $datetimes[] = esc_html( apply_filters( 'wc_bookings_summary_list_date', date_i18n( $date_format, $booking->get_start() ), $booking->get_start(), $booking->get_end() ) );
            }
        }

        if ( ! empty($datetimes) ) {
            // Output html
            echo '<ul class="wc-booking-summary-list"><li>' . implode('</li><li>', $datetimes) . '</li></ul>';
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement