在订单页面的列中显示 WooCommerce 预订开始/结束日期

问题描述 投票:0回答:1

我迅速浏览了 StackOverflow 上的其他五篇帖子,试图修改代码以获得正确答案。 其中一些看起来很有希望,但没有一个取得预期的结果。因此,我决定从头开始。

我的目标是:

  • 在 WooCommerce 订单页面上添加列。
  • 访问 mywebsite.com/wp-admin/edit.php?post_type=shop_order 并查看包含日期的列
  • 在该栏中包含预订开始和预订结束日期。
  • 每个订单始终只有一个预订

这是我迄今为止开发的代码,该代码将该列添加到订单页面:

// Add custom column to WooCommerce admin orders page
function custom_add_booking_dates_column($columns) {
    // Add the custom column after the "Order Actions" column
    $columns['booking_dates'] = __('Booking Dates', 'text-domain');
    return $columns;
}
add_filter('manage_edit-shop_order_columns', 'custom_add_booking_dates_column');

// Populate the custom column with "Testing" text
function custom_populate_booking_dates_column($column) {
    global $post;
    
    if ($column === 'booking_dates') {
        // Output the text you want to display in the custom column
        echo 'Testing';
    }
}
add_action('manage_shop_order_posts_custom_column', 'custom_populate_booking_dates_column');

我尝试过的事情:

  • 编辑 wp-config 并增加内存
  • 打开调试来解决(出现
    wc_booking_order_has_bookings
    错误)
  • 在子主题功能中加载 WooCommerce 和 WooCommerce Bookings
  • 将插件中的一些文件复制到子主题中并编辑
    /woocommerce-bookings/order/admin/booking-display.php
    booking-summary-list.php
    文件

我还尝试采用我在 SO 上找到的所有代码示例,并让 GPT 分析并编写一个函数,但生成的所有内容都不好。


// Add custom column to WooCommerce admin orders page
function custom_add_booking_dates_column($columns) {
    // Add the custom column after the 'order_actions' column
    $columns['booking_dates'] = __('Booking Dates', 'text-domain');
    return $columns;
}
add_filter('manage_edit-shop_order_columns', 'custom_add_booking_dates_column');

// Populate the custom column with data
function custom_booking_dates_column_content($column, $post_id) {
    if ($column == 'booking_dates') {
        // Retrieve booking dates for the order (replace this with your own logic)
        $booking_dates = get_post_meta($post_id, '_booking_dates', true);

        // Display the booking dates
        echo esc_html($booking_dates);
    }
}
add_action('manage_shop_order_posts_custom_column', 'custom_booking_dates_column_content', 10, 2);



add_action('after_setup_theme', 'child_theme_setup');

function child_theme_setup() {
    // Load WooCommerce
    add_theme_support('woocommerce');
    
    // Load WooCommerce Bookings
    add_theme_support('woocommerce-bookings');
}



// Add custom column to WooCommerce admin orders page
function add_custom_column_to_orders_page($columns) {
    // Add a new column called "Booking Dates" after the "Order Total" column
    $columns['booking_dates'] = __('Booking Dates', 'text-domain');
    return $columns;
}
add_filter('manage_edit-shop_order_columns', 'add_custom_column_to_orders_page', 20);

// Populate custom column with Booking Start and Booking End dates
function populate_custom_column_content($column) {
    global $post, $the_order;

    if ($column === 'booking_dates') {
        $order_id = $post->ID;

        // Check if the order has booking data
        if (wc_booking_order_has_bookings($order_id)) {
            $booking_data = wc_get_order($order_id)->get_items('booking');
            
            // Loop through each booking item (there may be multiple bookings in one order)
            foreach ($booking_data as $item_id => $booking_item) {
                $booking_start = date_i18n(wc_date_format(), strtotime($booking_item['Start']));
                $booking_end = date_i18n(wc_date_format(), strtotime($booking_item['End']));

                // Display Booking Start and End dates for each booking
                echo '<strong>' . __('Booking #', 'text-domain') . $booking_item['Booking ID'] . '</strong><br>';
                echo __('Start Date:', 'text-domain') . ' ' . esc_html($booking_start) . '<br>';
                echo __('End Date:', 'text-domain') . ' ' . esc_html($booking_end) . '<br>';
            }
        } else {
            echo __('No booking data', 'text-domain');
        }
    }
}
add_action('manage_shop_order_posts_custom_column', 'populate_custom_column_content');
php woocommerce orders woocommerce-bookings
1个回答
0
投票

请避免在此处使用 ChatGPT 生成的代码。尝试用以下代码替换您的代码:

// Add custom column to WooCommerce admin orders page
add_filter('manage_edit-shop_order_columns', 'add_custom_column_booking_dates', 20 );
function add_custom_column_booking_dates( $columns ) {
    $columns['booking_dates'] = __('Booking Dates', 'text-domain');
    return $columns;
}

// Custom column displayed content
add_action( 'manage_shop_order_posts_custom_column', 'display_custom_column_booking_dates_content', 20, 2 );
function display_custom_column_booking_dates_content( $column, $post_id ) {
    if ( 'booking_dates' === $column ) {
        global $post, $the_order;

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

        // Loop through order items
        foreach ( $order->get_items() as $item_id => $item ) {
            $booking_ids = WC_Booking_Data_Store::get_booking_ids_from_order_item_id( $item_id );

            if ( $booking_ids ) {
                foreach ( $booking_ids as $booking_id ) {
                    $booking       = new WC_Booking( $booking_id );
                    $booking_start = date_i18n( wc_date_format(), $booking->get_start());
                    $booking_end   = date_i18n( wc_date_format(), $booking->get_end());
                
                    // Display Booking Start and End dates for each booking
                    echo '<strong>' . __('Booking #', 'text-domain') . $booking_id . '</strong><br>';
                    echo __('Start Date:', 'text-domain') . ' ' . esc_html($booking_start) . '<br>';
                    echo __('End Date:', 'text-domain') . ' ' . esc_html($booking_end) . '<br>';
                }
            }
        }
        if ( ! $booking_ids ) {
            echo __('No booking data', 'text-domain');
        }
    }
}

应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.