【发布时间】:2021-08-25 06:16:05
【问题描述】:
我正在使用以下代码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;
};
不幸的是,这似乎不起作用,感谢任何帮助
【问题讨论】:
-
我已经更新了我的答案,请参阅编辑原因。问候
标签: php wordpress woocommerce backend coupon