1. Knowledge Base
  2. WooCommerce Product Table
  3. Developer Documentation

Hooks and filters

WooCommerce Product Table comes with a number of filters which allow you to customize the plugin’s behavior.

Please note that this code is aimed at developers and if you don’t know how to use it then you should ask your developer. If you don’t have one, we recommend posting a job on Codeable. We have partnered with them to provide plugin customizations for our customers.

Need professional assistance?

We've partnered with Codeable to provide our customers with expert help if required.

Filters

Language filters

There are various filters provided for manipulating text strings and messages used in the plugin. Please refer to this article for filters to override the text strings.

wc_product_table_custom_class

Use this to add a custom CSS class to every posts table. E.g.

add_filter( 'wc_product_table_custom_class', function( $class, $product_table ) {
    return 'my-class';
}, 10, 2 );

wc_product_table_column_class_<column>

Filters the column class used for all product tables. Expects an array of classes to be returned.

add_filter( 'wc_product_table_column_class_price', function( $class ) { 
    $class[] = 'super-discount';
    return $class;
} );

wc_product_table_row_class

Filters the row class used for each row in the table. Expects an array of classes to be returned. Takes two parameters – the classes array and the $product object.

add_filter( 'wc_product_table_row_class', function( $class, $product ) { 
    $class[] = 'trade';
    return $class;
}, 10, 2 );

wc_product_table_row_attributes

Filters the row attributes used for each product row in the table. Expects an array of attributes to be returned as key => value pairs. Takes two parameters – the classes array and the $product object.

add_filter( 'wc_product_table_row_attributes', function( $atts, $product ) { 
    $atts['data-foo'] = 'bar';
    return $atts;
}, 10, 2 );

wc_product_table_inline_config

Filters which allows you to override the inline table configuration used when the DataTables script is initialised. You can use this filter, for example, to disable or enable certain table features. The following example disables the ordering/sorting feature in all product tables:

// Disable column sorting
add_filter( 'wc_product_table_inline_config', function( $config, $product_table ) {
    $config['ordering'] = false;
    return $config;
}, 10, 2 );

wc_product_table_process_shortcodes

By default, WooCommerce Product Table will strip shortcodes from the main product description so any content that would appear inside these shortcodes will not appear in your table. If you wish to override this behavior globally (for all tables), return true from this filter. If you wish to enable it for just one table, use the shortcodes option.

add_filter( 'wc_product_table_process_shortcodes', '__return_true' );

wc_product_table_data_<column>

This filter is provided to allow you to modify the column data before it is added to the table. One filter is provided for each column in the table in this format: wc_product_table_data_<column>.

For example, to override the price column you would use the filter wc_product_table_data_price. To override data for the stock column, you would use the filter wc_product_table_data_stock, etc.

Any hyphens in the column name are replaced with underscores, so for the add-to-cart column the filter is: wc_product_table_data_add_to_cart.

Each of these filters takes two parameters: the data to add to the table, and the $product object (which is an instance of WC_Product).

add_filter( 'wc_product_table_data_name', function( $name, $product ) {
    return 'Item: ' . $name;
}, 10, 2 );

wc_product_table_data_attribute_<attribute>

This filter allows you to override data for an attribute column. Replace the <attribute> portion of the filter with the relevant attribute slug. For global attributes, the attribute slug is always prefixed with pa_, e.g. pa_color. So in this case the filter would be: wc_product_table_data_attribute_pa_color.

For product-specific (or “text”) attributes, you use the name defined in the Edit Product screen. So for product-specific attribute size, the filter would be: wc_product_table_data_attribute_size.

The filter accepts 2 parameters – the current attribute value and the current product object (WC_Product).

add_filter( 'wc_product_table_data_attribute_pa_color', function( $value, $product ) { 
    return 'Color: ' . $value; 
}, 10, 2 );

wc_product_table_data_taxonomy_<taxonomy>

This filter allows you to override data for a custom taxonomy column. Replace the <taxonomy> portion of the filter with the taxonomy slug. For example, if your taxonomy is called region, the filter would be wc_product_table_data_taxonomy_region.

The filter accepts 2 parameters – the current taxonomy value and the current product object (WC_Product).

add_filter( 'wc_product_table_data_taxonomy_region', function( $value, $product ) { 
    return 'Region: ' . $value; 
}, 10, 2 );

wc_product_table_data_custom_field

Filters which allows you to override the value of any custom field displayed in the table. It accepts 3 parameters – the custom field value, the custom field name, and the current product object (WC_Product).

// Override data for the 'extra_notes' custom field.
add_filter( 'wc_product_table_data_custom_field', function( $value, $field, $product ) {
    if ( 'extra_notes' === $field ) {
       // do something with $value
    }
    return $value;
}, 10, 3 );

wc_product_table_data_custom_field_<field_name>

Filters which allows you to override a specific custom field in the table. Replace <field_name> in the filter with the custom field you want to filter, e.g. wc_product_table_data_custom_field_some_field.

It accepts 2 parameters – the custom field value and the current product object (WC_Product).

// Override data for the 'extra_notes' custom field.
add_filter( 'wc_product_table_data_custom_field_extra_notes', function( $value, $product ) {
    $value = 'xyz'; // example 
    return $value;
}, 10, 2 );

wc_product_table_acf_value

Filters which allows you to override the custom field value returned from Advanced Custom Fields. It accepts 3 parameters – the custom field value, the ACF field object and the current post (i.e. product) ID.

add_filter( 'wc_product_table_acf_value', function( $value, $field_obj, $post_id ) {
    if ( 'file' === $field_obj['type'] ) {
       // do something with $value
    }
    return $value;
}, 10, 3 );

wc_product_table_custom_field_stored_date_format

If you are using date-based custom fields in your table, you may find that your dates are sometimes incorrectly sorted or formatted. The most likely cause of this is the date conversion functions used by the plugin which are built into PHP.

For example, dates such as “12/04/2018” are assumed to be in U.S. format with the month first (m/d/Y). So this example date would be December 4th, 2018.

If the date is separated by dash (-), dot (.) or other symbol, it’s assumed to be a European/Australian date with the day before the month. If you’re using dates separated by a forward slash but in this format: d/m/Y, the plugin will not be able to sort these correctly (or format them if you’re using the date_format option). To fix this, you will need to set this filter.

Note: this is not related to how the custom field is displayed in the table – see the date_format option for controlling the output of date custom fields.

add_filter( 'wc_product_table_custom_field_stored_date_format', function( $format, $field ) { 
    if ( 'despatch_date' === $field ) {
        return 'd/m/Y';
    }
    return ''; 
}, 10, 2 );

wc_product_table_custom_field_is_eu_au_date

An alternative to the above filter, you can use this shorthand filter to quickly indicate your field is stored in European/Australian format (i.e. day before month).

The filter can be applied globally to all date-based custom fields, as follows:

​add_filter( 'wc_product_table_custom_field_is_eu_au_date', '__return_true' );

Or you can use it for individual fields only, like this:

​add_filter( 'wc_product_table_custom_field_is_eu_au_date', function( $is_eu_date, $field ) {
    if ( 'event_date' === $field ) {
        return true;
    }
    return false;
}, 10, 2 );

wc_product_table_separator_<type>

Filter which allows you to change the separator used between categories, tags, terms, attributes and custom fields. For example, a product can have multiple categories, each separated by a comma. This filter allows you to change the comma for a different character, line breaks, or other custom HTML.

The filter must include the relevant type. Possible values for type are: categories, tags, terms, attributescustom_field, and custom_field_row.

For example, to change the separator between categories in the table, add this filter:

add_filter( 'wc_product_table_separator_categories', function( $sep ) {
    return '<br/>';
} );

wc_product_table_column_heading_<column>

Filters the column heading for the specified column for all product tables on your site. If a custom heading is specified within the shortcode itself (in the columns option), then it will take precedence over the value returned from this filter.

// Override the 'name' column heading
add_filter( 'wc_product_table_column_heading_name', function( $heading ) {
    return 'Item Name';
} );

wc_product_table_column_priority_<column>

Filters the column priority for all product tables. If a priority is specified within the shortcode itself (using the priorities option), then it will take precedence over the value returned from this filter.

add_filter( 'wc_product_table_column_priority_date', function( $priority ) {
    return 10; // a high number means low priority
} );

wc_product_table_column_width_<column>

Filter which allows you to override a column width for all product tables on your site. If a width is specified within the shortcode itself (using the widths option), then it will take priority over the value returned from this filter.

add_filter( 'wc_product_table_column_width_sku', function( $width ) {
    return '100px';
} );

wc_product_table_column_searchable_<column>

This filter allows you to set whether or not a column is “searchable” in the product table. If a column is searchable, it means that the column will be used when the user types something into the search box above the table.

By default, the plugin includes all columns for searching, except for the image column (unless you’re using the lazy load option, in which case only the title and content columns are searchable). To override this behavior when you’re not using lazy load, use this filter with the appropriate column name added to the filter.

E.g. to exclude the price column from the table search:

add_filter( 'wc_product_table_column_searchable_price', '__return_false' );

If you want to disable searching for custom fields, attributes or taxonomies, use the column name without the cf:, att: or tax: prefix:

add_filter( 'wc_product_table_column_searchable_my_custom_field', '__return_false' );

wc_product_table_column_sortable_<column>

This filter allows you to set whether or not a column is “sortable” in the product table. If a column is sortable, the table can be re-ordered by that column using the up/down arrows that appear in the column heading.

If you’re using lazy load, the sorting is restricted to certain columns only. If you’re not using lazy load, then sorting is enabled for all columns except image, add-to-cart and button.

E.g. to prevent the table being sorted by price:

add_filter( 'wc_product_table_column_sortable_price', '__return_false' );

See the note above about using this filter for custom field, attribute or taxonomy columns.

wc_product_table_max_product_limit

Filter which allows you to override the default products limit (500 products) set by WooCommerce Product Table. This will override the option in the plugin settings page. Note that this option only applies when you are not using the lazy_load option.

add_filter( 'wc_product_table_max_product_limit', function( $limit, $product_table ) {
    return 1000;
}, 10, 2 );

wc_product_table_data_cache_expiry

The length of time in seconds that the product table data will be cached. Defaults to 24 * HOUR_IN_SECONDS (1 day).

wc_product_table_use_data_cache

Filter whether to use data caching globally across all product tables. Should return true or false.

wc_product_table_query_args

Filters the query args passed to WP_Query prior to retrieving products from the database. See WP_Query documentation for details. Takes two parameters – the query args ($args) and the WC_Product_Table_Query instance.

add_filter( 'wc_product_table_query_args', function( $args, $wpt_query ) {
    // do something with $args
    return $args;
}, 10, 2 );

wc_product_table_meta_query

Filter the meta query used in building the product table. Takes two parameters – the meta_query array and the WC_Product_Table_Query instance.

wc_product_table_tax_query

Filter the tax query used in building the product table. Takes two parameters – the tax_query array and the WC_Product_Table_Query instance.

wc_product_table_optimize_table_query

Filter whether the product table query should be optimized. Defaults to true.

Optimization involves removing the content and excerpt columns from the wp_posts query to reduce the amount of data returned from the database. The optimization only occurs if the description or short-description columns are used.

add_filter( 'wc_product_table_optimize_table_query', '__return_false' );

wc_product_table_use_numeric_skus

By default, WooCommerce Product Table will use alphabetical sorting for SKUs. If you’re store uses purely numerical SKUs (e.g. 256, 321, etc), you will need to enable numerical SKUs using this filter, in order for the column sorting to work correctly. Add the following code:

​add_filter( 'wc_product_table_use_numeric_skus', '__return_true' );

wc_product_table_search_filter_get_terms_args

Filters the arguments passed to get_terms when retrieving the lists of terms (i.e. categories, tags, attribute or taxonomy terms) for the search filter dropdown menus. The filter passes an array of arguments – see get_terms for details.

wc_product_table_search_filter_terms_<taxonomy>

Filters the list of items shown in the search filter dropdown menu for the given taxonomy. The dynamic portion of the filter is the taxonomy name (e.g. product_cat, pa_color, etc). for the filter dropdown. The filter passes an array of WP_Term objects.

wc_product_table_get_table_output

Allows you to modify the result of the WC_Product_Table->get_table() function. Depending on the return type, the value passed to this filter could be an HTML_Data_Table instance, a HTML string, and array or JSON. It takes 3 parameters: the result, the output type, and the WC_Product_Table instance.

add_filter( 'wc_product_table_get_table_output', function( $result, $output_type, $table ) {
    // Assuming get_data() was called with 'object' return type
    if ( 'object' === $output_type ) {
        $result->add_attribute( 'data-foo', 'bar' );
    }
    return $result;
}, 10, 3 );

wc_product_table_shortcode_output

Allows you to modify the HTML returned by the [wc_product_table] shortcode. It takes 2 parameters: the HTML and the WC_Product_Table instance.

add_filter( 'wc_product_table_shortcode_output', function( $html, $table ) {
    // do something with HTML
    return $html;
}, 10, 2 );

wc_product_table_multi_cart_class

Allows you to change the button class used for the multi add to cart button. Defaults to ‘button’.

add_filter( 'wc_product_table_multi_cart_class', function( $class ) {
    return "btn";
} );

wc_product_table_multi_add_to_cart_data

Filters the data used for the ‘Add Selected to Cart’ form which adds multiple products to the cart based on the selected products. Expects an array of key => value pairs, where the key name is the name given to the hidden field input to be included in the cart data.

add_filter( 'wc_product_table_multi_add_to_cart_data', function( $data ) { 
    $data['foo'] = 'bar';
    return $data; 
} );

wc_product_table_open_products_in_new_tab

Product tables contain various links to the product details page. For example, the name column will (by default) include a link to the product page.

These will open in the same tab or window by default. To open them in a new tab use the wc_product_table_open_products_in_new_tab filter. For example:

add_filter( 'wc_product_table_open_products_in_new_tab', '__return_true' );

wc_product_table_use_fitvids

Whether to use the FitVids library to make videos in the table responsive. Defaults to true.

Actions

wc_product_table_parse_args

Fired when the product table arguments have been parsed and set. Takes one argument – the WC_Product_Table_Args instance.

wc_product_table_before_get_table

Fired once before each table is created on the page. Takes one argument – the WC_Product_Table instance.

wc_product_table_after_get_table

Fired once after each table is created on the page. Takes one argument – the WC_Product_Table instance.

wc_product_table_before_get_data

Fired once before the data is fetched and added to the table. Takes one argument – the WC_Product_Table instance.

wc_product_table_after_get_data

Fired once after the data is added to the table. Takes one argument – the WC_Product_Table instance.

Still need help?

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

Related Articles