在 WooCommerce 管理员优惠券列表的新列中显示优惠券产生的总销售额
Display the total amount of sales generated by coupon in a new column on WooCommerce admin coupon list
我正在使用下面的代码 https://www.businessbloomer.com/woocommerce-calculate-sales-coupon-code/ 来显示总数
新标签页中给定优惠券代码产生的销售额
WooCommerce“报告”。
/**
* @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;
}
但是,我的意图是显示产生的总销售额
通过 WooCommerce 管理员优惠券列表新列中的优惠券
所以我想出了:
// 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;
};
不幸的是,这似乎不起作用,感谢您的帮助
在您的代码中缺少 manage_edit-shop_coupon_columns
过滤器挂钩,它允许您在 WooCommerce 管理员优惠券列表上创建一个新列。
然后 manage_shop_coupon_posts_custom_column
操作挂钩允许您向新列添加内容。
因此,要在 WooCommerce 管理员优惠券列表的新列中显示优惠券产生的销售总额,请使用:
编辑:
在我发布我的第一个答案后,我意识到你得到的订单越多,就会越重......而直接 SQL 查询会轻得多。
因为您不必重新发明热水,所以我在 答案代码中找到了完美的解决方案。
新答案:
// 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 );
上一个答案: 这也有效,但随着时间的推移会产生重大影响
// 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 );
相关:
我正在使用下面的代码 https://www.businessbloomer.com/woocommerce-calculate-sales-coupon-code/ 来显示总数 新标签页中给定优惠券代码产生的销售额 WooCommerce“报告”。
/**
* @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;
}
但是,我的意图是显示产生的总销售额 通过 WooCommerce 管理员优惠券列表新列中的优惠券
所以我想出了:
// 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;
};
不幸的是,这似乎不起作用,感谢您的帮助
在您的代码中缺少 manage_edit-shop_coupon_columns
过滤器挂钩,它允许您在 WooCommerce 管理员优惠券列表上创建一个新列。
然后 manage_shop_coupon_posts_custom_column
操作挂钩允许您向新列添加内容。
因此,要在 WooCommerce 管理员优惠券列表的新列中显示优惠券产生的销售总额,请使用:
编辑:
在我发布我的第一个答案后,我意识到你得到的订单越多,就会越重......而直接 SQL 查询会轻得多。
因为您不必重新发明热水,所以我在
新答案:
// 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 );
上一个答案: 这也有效,但随着时间的推移会产生重大影响
// 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 );
相关: