1. WooCommerce Product Table
  2. Developer Documentation

Adding a custom column to the product table

These instructions will explain how to add a custom column in WooCommerce Product Table. Custom columns are a good way to display data programmatically which is not available in the standard table columns.

Need professional assistance?
We've partnered with Codeable to provide our customers with expert help if required.

If you would prefer an alternative "work around" that doesn't require programming, you could create a custom field, display it as a column, and add the data for each product manually.


To add a custom column to your table you need to do the following:

  1. Add the column to the columns option in the product table. This can added to the settings page, or directly in the shortcode. The column can be called anything you like, e.g. sale_price, wholesale, file_type, etc. It doesn't matter what the name of the column is as long as you don't use an existing column name.
  2. Create a new class that implements Table_Data_Interface. This is the class that will retrieve the actual data for your custom column.
  3. Add a hook to your theme's functions.php file or to a custom plugin, which creates an instance of the class in Step 2.

Worked example

For this example we're going to assume the custom column we're adding is called wholesale_price.

1. Adding the column to the options or shortcode

In this example we're going to add the column directly in our shortcode, like this:

[product_table columns="name,description,wholesale_price,buy"]

2. Creating the data class

Each column in the table has an associated class which implements Barn2\WPT_Lib\Table\Table_Data_Interface. To add a custom column, you will need to create a class which implements this interface.

Alternatively, you can extend the Abstract_Product_Data class which implements Table_Data_Interface. You then just need to override the get_data() method in your class.

Here's an example class for our wholesale_price column:

use Barn2\Plugin\WC_Product_Table\Data\Abstract_Product_Data,
    Barn2\Plugin\WC_Product_Table\Table_Args;

if ( ! class_exists( 'Barn2\Plugin\WC_Product_Table\Data\Abstract_Product_Data' ) ) {
    return;
}

/**
 * Handles data retrieval for the 'wholesale_price' column in the product table.
 */
class Product_Table_Wholesale_Price_Column extends Abstract_Product_Data {

    public function get_data() {
        return 'This is my data';
    }

}

3. Adding the data class hook

The hook you need to add is: wc_product_table_custom_table_data_<column>. This filter should return an instance of your data class.

So for our wholesale_price example, the filter would be wc_product_table_custom_table_data_wholesale_price. The hook would be as follows:

add_filter( 'wc_product_table_custom_table_data_wholesale_price', function( $obj, WC_Product $product, Table_Args $table_args ) { 
    return new Product_Table_Wholesale_Price_Column( $product ); 
}, 10, 3 );

Once you've added the code in steps 2 and 3, you should see data for your custom column displayed in the table.

Setting the column heading

The product table will give your column a heading based on its name. So in our example the heading would be "Wholesale Price". If you want a different column heading, you can set this in the usual way by adding a : (colon) after the column followed by the heading:

[product_table columns="name,description,wholesale_price:Price,buy"]

A note on caching

If the data for your custom column returns unexpected or stale data, please check your caching settings as you may need to disable or flush the cache.

Was this article helpful?

Related Articles

Still need help?
If searching the knowledge base hasn't answered your question, please contact support.