在 Woocommerce 管理订单列表中显示预订日期时间

问题描述

在 Woocommerce with Woocommerce Bookings 中,我在 WooCommerce 订单预览中添加了以下预订详细信息,在每个订单项下方:

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',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',$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',$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',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;
}

如何在 woocommerce 管理订单列表中的 billing_address 列下显示预订日期和时间(不是订单日期和时间)?

感谢任何帮助。

enter image description here

解决方法

您可以使用以下内容在 woocommerce 管理订单列表中的 billing_address 列下显示预订日期和时间:

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>';
        }
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经测试有效。