Thêm cột tùy chỉnh tại Bảng danh sách đơn hàng WooCommerce

Tôi đang điều hành một trang web thành viên và muốn thêm một cột mới vào bảng danh sách đơn hàng của mình (Bảng điều khiển WordPress). Tôi sẽ hiển thị tuổi của thành viên tại cột đó. Đây là các bước của tôi cho chức năng này:

Tạo trường tùy chỉnh

Tôi đang lưu Ngày sinh của khách hàng của mình dưới dạng dữ liệu trường tùy chỉnh. Tôi đang sử dụng tùy chọn trường tùy chỉnh gốc của WordPress. Bạn cũng có thể sử dụng plugin bên thứ ba miễn phí như ACF / Pods / CMB2, v.v.

Bây giờ tôi sẽ thêm một số đoạn mã PHP vào tệp functions.php của chủ đề đang hoạt động của tôi.

Thêm cột mới vào bảng danh sách đơn hàng

Bước đầu tiên là hook vào load-edit.php , nó sẽ kích hoạt bất cứ khi nào bảng danh sách đơn hàng của cửa hàng đang tải. Tôi sẽ sử dụng get_current_screen để lấy đối tượng màn hình và đảm bảo rằng tôi đang ở trên bảng danh sách thứ tự.
add_action('load-edit.php', 'wco_load', 20);
function wco_load() {
    $screen = get_current_screen();
    if (!isset($screen->post_type) || 'shop_order' != $screen->post_type) {
        return;
    }
    //* Adding the custom column into the existing List Table Column
    add_filter("manage_{$screen->id}_columns", 'wco_add_columns');
}
function wco_add_columns( $columns ) {
	$actions = $columns['order_actions'];
	$order_total = $columns['order_total'];
	$order_date = $columns['order_date'];
	unset($columns['order_date']);
	unset($columns['order_total']);
	unset($columns['order_actions']);
	$columns["dob_field"] = __( "Age", "themeprefix");
	$columns['order_date'] = $order_date;
	$columns['order_total'] = $order_total;
	$columns['order_actions'] = $actions;
    return $columns;
}

Hiển thị Nội dung trong Cột Tùy chỉnh

Để hiển thị nội dung, chúng tôi kết nối vào quản lý _ {$ screen-> post_type} _posts_custom_column . Tôi sẽ sửa đổi chức năng tải của mình một chút:
add_action('load-edit.php', 'wco_load', 20);
function wco_load() {
	$screen = get_current_screen();
	if (!isset($screen->post_type) || 'shop_order' != $screen->post_type) {
		return;
	}
	//* Adding the custom column into the existing List Table Column
	add_filter("manage_{$screen->id}_columns", 'wco_add_columns');
	//* Displaying Content in the Custom Column
	add_action( "manage_{$screen->post_type}_posts_custom_column", 'wco_column_cb_data', 10, 2 );
}
function wco_column_cb_data( $colname, $cptid ) {
	if ( $colname == 'dob_field') {
		$dob = get_post_meta( $cptid, 'dob_field', true );
		if( ! empty( $dob ) ) {
			$age = date('Y') - date( 'Y', strtotime($dob) );
			echo $age;
		}
	}
}
Đây là mã hoàn chỉnh. Bạn sẽ đặt mã này vào tệp functions.php của mình :
//* Adding the custom column into the existing List Table Column
	add_filter("manage_{$screen->id}_columns", 'wco_add_columns');
	//* Displaying Content in the Custom Column
	add_action( "manage_{$screen->post_type}_posts_custom_column", 'wco_column_cb_data', 10, 2 );
}
function wco_add_columns( $columns ) {
	$actions = $columns['order_actions'];
	$order_total = $columns['order_total'];
	$order_date = $columns['order_date'];
	unset($columns['order_date']);
	unset($columns['order_total']);
	unset($columns['order_actions']);
	$columns["dob_field"] = __( "Age", "themeprefix");
	$columns['order_date'] = $order_date;
	$columns['order_total'] = $order_total;
	$columns['order_actions'] = $actions;
    return $columns;
}
function wco_column_cb_data( $colname, $cptid ) {
	if ( $colname == 'dob_field') {
		$dob = get_post_meta( $cptid, 'dob_field', true );
		if( ! empty( $dob ) ) {
			$age = date('Y') - date( 'Y', strtotime($dob) );
			echo $age;
		}
	}
}
Cảm thấy bình luận miễn phí ở bên dưới. Nếu bạn cần bất kỳ trợ giúp nâng cao nào, vui lòng điền vào biểu mẫu bên dưới.