How to Add an Extra Column to a WooCommerce Product Table

How to Add an Extra Column to a WooCommerce Product Table

What needs to be done to add an extra column to the product list table in a WordPress store? In fact, adding new columns in the Woo plugin is no different from adding columns to the regular post table.

To achieve this, we’ll need to use two dynamic hooks:

  • manage_{post_type}_posts_columns — used to add the column header (in HTML table structure: table → thead → th → td → column title);
  • manage_{post_type}_posts_custom_column — used to fill table cells for products with additional content (in HTML table structure: table → tbody → tr → td → cell content).

Example

As an example, let’s add a column displaying the shop that owns the product. To do that, we add a filter named 'manage_product_posts_columns'. Here, “product” is the post type for WooCommerce products:

add_filter('manage_product_posts_columns', function($defaults)
{
	$columns = [];
	foreach($defaults as $field => $value)
	{
		if($field == 'date')
		{
			$columns['shop'] = __('Shop', 'xyz');
		}
		$columns[$field] = $value;
	}
	
	return $columns;
}, 100);

In the example above, we used a loop to insert the column before the date column instead of appending it to the end.

Next, we need to populate the table cells with data. For that, we use the action hook 'manage_product_posts_custom_column'. Again, “product” is the WooCommerce product post type. The first parameter of the function is the name of the column (there may be multiple), and the second is the post ID of the current row.

add_action('manage_product_posts_custom_column', function($column_name, $post_ID)
{
	switch($column_name)
	{
		case 'shop':
			$shop_id = get_post_meta($post_ID, 'shop_id', true);
			
			echo $shop_id ? $shop_id : __('Shop not found', ‘xyz’);
		break;
	}
}, 10, 2);

In this example, we retrieve data from the post’s meta field with the key 'shop_id'. If there is data in the database, we display the shop ID; otherwise, we display a fallback message.

Are you having problems with your WordPress site? Do you need additional functionality? A custom plugin or a new page?
Then write to me via the feedback form, and I will try to help you.

Write a comment

Your email address will not be published. Required fields are marked *