For every coupon I have defined in the system I would like to show usage statistics: how much sales it was used with, how much discount it provided, etc... I would like to add that data on that coupon's edit page in the admin (either as a new tab or a metabox)
So I have the code to count all sales used that coupon. But how do I add it to the coupon edit page in woocommerce admin
function get_sales_by_coupon($coupon_id) {
$args = [
'post_type' => 'shop_order',
'posts_per_page' => '-1',
'post_status' => ['wc-processing', 'wc-completed']
];
$my_query = new WP_Query($args);
$orders = $my_query->posts;
$total = 0;
foreach ($orders as $key => $value) {
$order_id = $value->ID;
$order = wc_get_order($order_id);
$items = $order->get_items('coupon');
foreach ( $items as $item ) {
if( $item['code'] == $coupon_id ) {
$total += $order->get_total();
}
}
}
return 'Total sales for coupon "' . $coupon_id . '": ' . wc_price($total);
}