
In order to add a column showing the income obtained by each Woocommerce coupon, we must use a filter and an action:
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(
$wpdb->prepare("
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 =
{$wpdb->prefix}wc_order_product_lookup.order_id
WHERE {$wpdb->prefix}wc_order_coupon_lookup.coupon_id = %d",
$coupon_id
)
)[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 );