Skip to content

Integrate earned revenue column in Woocommerce coupons

In order to add a column showing the income obtained by each Woocommerce coupon, we must use a filter and an action:

  • manage_edit-shop_coupon_columns:
    With this filter we have access to the columns of the Woocommerce coupons table that we find in the WordPress administration page (wp-admin). Through this filter we will add a new column of “earned income”.
  • manage_shop_coupon_posts_custom_column:
    With this action we can add custom content to the columns of the Woocommerce coupon table. This action has two parameters: the first refers to the name of the current column, while the second contains the ID of the current coupon.

Below is the snippet to integrate this new column:

// Auxiliary function to obtain the total sales of a coupon from its ID
function get_coupon_total_sales( $coupon_id ) {
global $wpdb;
return $wpdb->get_col(
    	SELECT SUM({$wpdb->prefix}wc_order_product_lookup.product_net_revenue)
    	FROM {$wpdb->prefix}wc_order_coupon_lookup
    	INNER JOIN {$wpdb->prefix}wc_order_product_lookup
    	ON {$wpdb->prefix}wc_order_coupon_lookup.order_id =
    	WHERE {$wpdb->prefix}wc_order_coupon_lookup.coupon_id = %d",
    )[0] ?? 0;
// We add the new column to the coupon table
add_filter( 'manage_edit-shop_coupon_columns', function( $columns ) {
	$columns['total_sales'] = "Ingresos obtenidos";
	return $columns;
} );

// We add the content of the new column created
add_action( 'manage_shop_coupon_posts_custom_column', function( $column, $coupon_id ) {
	if ( $column == 'total_sales' ) {
	    echo wc_price( get_coupon_total_sales( $coupon_id ) );
}, $priority = 10, $accepted_args = 2 );