display total sales of coupon in frontend wordpress

Viewed 16

I'm using this snippets to show the total sales of coupon in admin dashboard

/**
 * @snippet       Total Sales By Coupon @ WooCommerce Admin
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
// -------------------------
// 1. Create function that calculates sales based on coupon code
  
function bbloomer_get_sales_by_coupon( $coupon_code ) {
   global $wpdb;
    $total = $wpdb->get_var( "
        SELECT SUM(pm.meta_value)
        FROM $wpdb->posts p
      INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_items as oi ON p.ID = oi.order_id
        WHERE p.post_type = 'shop_order'
      AND pm.meta_key = '_order_total'
        AND p.post_status IN ( 'wc-completed', 'wc-processing')
        AND oi.order_item_type = 'coupon'
        AND oi.order_item_name LIKE '" . $coupon_code . "'
    " );
   return wc_price( $total );
}
  
// -------------------------
// 2. Add new column to WooCommerce Coupon admin table with total sales
  
add_filter( 'manage_edit-shop_coupon_columns', 'bbloomer_admin_shop_coupon_sales_column', 9999 );
  
function bbloomer_admin_shop_coupon_sales_column( $columns ) {
   $columns['totsales'] = 'Total Sales';
   return $columns;
}
  
add_action( 'manage_shop_coupon_posts_custom_column', 'bbloomer_admin_shop_coupon_sales_column_content', 9999, 2 );
  
function bbloomer_admin_shop_coupon_sales_column_content( $column, $coupon_id ) {
    if ( $column == 'totsales' ) {
      echo bbloomer_get_sales_by_coupon( get_the_title( $coupon_id ) );
    }
}

Plus I'm using this snippetes to show the number of coupon usage in FRONT END

function simple_function_1() {
    
    $coupon_code = 'coupon-test';
    global $woocommerce;
    $coupon_data = new WC_Coupon($coupon_code);
    echo ($coupon_data->usage_count);// return number of remaining coupons
}
add_shortcode( 'own_shortcode1', 'simple_function_1' );

I need to modify the second snippet FRONTEND snippet to show the number of products sold (OR) number of sales generated by coupon

0 Answers
Related