I am using the following code https://www.businessbloomer.com/woocommerce-calculate-sales-coupon-code/ that allows me to display the total amount of sales generated by a given coupon code in a new tab on WooCommerce "Reports".
/**
* @snippet Get Total Sales by COUPON
* @how-to Get CustomizeWoo.com FREE
* @sourcecode https://businessbloomer.com/?p=72576
* @author Rodolfo Melogli
* @testedwith WooCommerce 3.0.7
*/
// -------------------------
// 1. Create function that calculates sales based on coupon code
function bbloomer_get_sales_by_coupon($coupon_id) {
$args = [
'post_type' => 'shop_order',
'posts_per_page' => '-1',
'post_status' => ['wc-processing', 'wc-completed', 'wc-on-hold']
];
$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);
}
// -------------------------
// 2. Add new tab to WooCommerce "Reports", and print the coupon total sales
add_filter( 'woocommerce_admin_reports', 'bbloomer_add_report_tab' );
function bbloomer_add_report_tab( $reports ) {
$reports['coupons'] = array(
'title' => __( 'Coupons', 'woocommerce' ),
'reports' => array(
"sales_by_code" => array(
'title' => __( 'Sales by code', 'woocommerce' ),
'description' => bbloomer_get_sales_by_coupon('barmada'), //change coupon code here
'hide_title' => false,
'callback' => '',
),
),
);
return $reports;
}
However, my intention is to display the total amount of sales generated by coupon in a new column on WooCommerce admin coupon list
So i came up with:
// add the action
add_action( 'manage_shop_coupon_posts_custom_column', 'my_callback_function', 10, 2 );
// define the manage_shop_coupon_posts_custom_column callback
function my_callback_function( $array, $int ) {
// make action magic happen here...
$array['coupons'] = array(
'title' => __( 'Coupons', 'woocommerce' ),
'reports' => array(
"sales_by_code" => array(
'title' => __( 'Sales by code', 'woocommerce' ),
'description' =>
bbloomer_get_sales_by_coupon('barmada'), //change coupon code here
'hide_title' => false,
'callback' => '',
),
),
);
return $array;
};
Unfortunately this doesn't seem to work, any help is appreciated
In your code the manage_edit-shop_coupon_columns
filter hook is missing, which allows you to create a new column on WooCommerce admin coupon list.
The manage_shop_coupon_posts_custom_column
action hook then allows you to add content to the new column.
So to display the total amount of sales generated by coupon in a new column on WooCommerce admin coupon list, use:
EDIT:
After I posted my first answer, I realized that the more orders you will get, more is going to be heavy… and a direct SQL query would be much lighter.
Because you don't have to reinvent the hot water, I found in Display custom data on Woocommerce admin coupon edit pages answer code the perfect solution.
New answer:
// Get totals orders sum for a coupon code
function get_total_sales_by_coupon( $coupon_code ) {
global $wpdb;
return (float) $wpdb->get_var( $wpdb->prepare("
SELECT SUM( pm.meta_value )
FROM {$wpdb->prefix}postmeta pm
INNER JOIN {$wpdb->prefix}posts p
ON pm.post_id = p.ID
INNER JOIN {$wpdb->prefix}woocommerce_order_items woi
ON woi.order_id = pm.post_id
WHERE pm.meta_key = '_order_total'
AND p.post_status IN ( 'wc-processing', 'wc-completed', 'wc-on-hold' )
AND woi.order_item_name LIKE '%s'
AND woi.order_item_type = 'coupon'
", $coupon_code ) );
}
// Add a Header
function filter_manage_edit_shop_coupon_columns( $columns ) {
// Add new column
$columns['total_sales'] = __( 'Total sales', 'woocommerce' );
return $columns;
}
add_filter( 'manage_edit-shop_coupon_columns', 'filter_manage_edit_shop_coupon_columns', 10, 1 );
// Populate the Column
function action_manage_shop_coupon_posts_custom_column( $column, $post_id ) {
// Compare
if ( $column == 'total_sales' ) {
// Call function
$total = get_total_sales_by_coupon( get_the_title( $post_id ) );
// Output
echo wc_price( $total );
}
}
add_action( 'manage_shop_coupon_posts_custom_column' , 'action_manage_shop_coupon_posts_custom_column', 10, 2 );
Previous answer: which also works but would have a heavy impact over time
// Add a Header
function filter_manage_edit_shop_coupon_columns( $columns ) {
// Add new column
$columns['total_sales'] = __( 'Total sales', 'woocommerce' );
return $columns;
}
add_filter( 'manage_edit-shop_coupon_columns', 'filter_manage_edit_shop_coupon_columns', 10, 1 );
// Populate the Column
function action_manage_shop_coupon_posts_custom_column( $column, $post_id ) {
// Compare
if ( $column == 'total_sales' ) {
// Get ALL orders with certain status and meta_key '_cart_discount' > 0 (coupon used in order)
// NOTE THE USE OF WC-.. at the order status
$orders = wc_get_orders( array(
'status' => array( 'wc-processing', 'wc-completed', 'wc-on-hold' ),
'meta_key' => ' _cart_discount',
'meta_value' => 0,
'meta_compare' => '>',
));
// NOT empty
if ( sizeof( $orders ) > 0 ) {
// Set variable
$total = 0;
// Iterating through each order
foreach ( $orders as $order ) {
// Loop through WC_Order_Item_Coupon objects
foreach ( $order->get_coupon_codes() as $coupon_code ) {
// Retrieving the coupon ID
$coupon_post_obj = get_page_by_title( $coupon_code, OBJECT, 'shop_coupon' );
$coupon_id = $coupon_post_obj->ID;
// Compare
if ( $coupon_id == $post_id ) {
// Add to total
$total += $order->get_total();
}
}
}
// Output
echo wc_price( $total );
}
}
}
add_action( 'manage_shop_coupon_posts_custom_column' , 'action_manage_shop_coupon_posts_custom_column', 10, 2 );
Related: Add a new column with author name to WooCommerce admin coupon list
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.