Docs Email Reports for WooCommerce Custom Columns for Top Selling Products

Custom Columns for Top Selling Products

This example adds Stock Status and Price columns to the Top Selling Products table.

Before you start

  • Email Reports 3.2 or later. The Top Selling Products column filters were added in 3.2.
  • The Top Selling Products table must be enabled on the report, in the Report Details panel. If the table is off, the filters never run.
  • Somewhere to put custom PHP — a site-specific plugin, a code snippets plugin, or your child theme’s functions.php.
  • A backup, and ideally a staging site to try it on first.

The code

// Add the column headers.
add_filter( 'sre_top_selling_products_details_table_header', 'my_top_products_header', 10, 1 );

function my_top_products_header( $th_array ) {
	$th_array[] = 'Stock Status';
	$th_array[] = 'Price';
	return $th_array;
}

// Add the matching cell for each product row.
add_filter( 'sre_top_selling_products_details_table_content', 'my_top_products_content', 10, 3 );

function my_top_products_content( $array, $top_seller, $product_id ) {

	if ( ! is_array( $array ) ) {
		return $array;
	}

	$stock_status = isset( $top_seller['extended_info']['stock_status'] ) ? $top_seller['extended_info']['stock_status'] : 'N/A';
	$price        = isset( $top_seller['extended_info']['price'] ) ? $top_seller['extended_info']['price'] : 'N/A';

	$array[] = $stock_status;
	$array[] = $price;

	return $array;
}
Name your callbacks something of your own
Do not name the callback function the same as the filter it hooks to. Earlier versions of this example did, which works but makes the code confusing and risks colliding with another snippet. Prefix your functions, as above.

How it works

FilterWhat it receivesWhat to return
sre_top_selling_products_details_table_headerThe array of column headings.The same array with your headings appended.
sre_top_selling_products_details_table_contentThe array of cells for one row, the $top_seller data for that product, and its $product_id. Note it takes three arguments, so register it with a priority and an argument count of 3.The same array with your cell values appended, in the same order as the headings.

Product details come from $top_seller['extended_info'], which is WooCommerce Analytics data. If a field you want isn’t there, use the $product_id to load the product yourself with wc_get_product().

This example shows the product’s stock status — In stock, Out of stock, On backorder — not the stock quantity. For the number in stock, load the product and call get_stock_quantity().

Testing it

Send yourself a test report — it uses live data, so the new columns show real values immediately.

Troubleshooting

ProblemCheck
The columns don’t appear at allThe Top Selling Products table is enabled on this report, and you’re running Email Reports 3.2 or later.
Cells show N/Aextended_info doesn’t carry that field for those products. Load the product with wc_get_product( $product_id ) instead.
A PHP error about the number of argumentsThe content filter needs 10, 3 on add_filter — it passes three arguments, not one.
Columns are misalignedYou added a different number of header cells and body cells, or added them in a different order.

See also: Custom Columns for Order Details Report.