Adding Custom Column at WooCommerce Order List Table

I am running a membership site and wanting to add an new column at my order listing table (WordPress Dashboard). I shall show the member’s age at that column. Here is my steps for this functionality:

Creating Custom Field

I’m saving the Date of Birth of my customers as a custom field data. I’m using WordPress’s native custom field option. You can also use the free third party plugin like ACF/Pods/CMB2 etc.

Now I shall add some PHP snippets into my functions.php file of my active theme.

Adding New Column into the Order List Table

The first step is to hook into load-edit.php, which will fire whenever shop order list table is loading. I’ll use get_current_screen to grab the screen object and make sure I’m on the order list table.

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

Displaying Content in the Custom Column

To display content, we hook into manage_{$screen->post_type}_posts_custom_column. I’ll modify my load function a bit:

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

	}
}

Here is the complete code. You will put this code into your functions.php file:

   //* 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;
		}

	}
}

Feel free comments at below. If you need any advanced help, please fill up the form at below.

This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.