带有折扣的订单的Wordpress报告

时间:2019-04-07 12:08:30

标签: php wordpress woocommerce hook-woocommerce

您好,我想在wp-admin中进行报告,该报告显示具有折扣的订单的以下详细信息:
  [订单号,订单日期,订单状态,订单总数,用户名,电子邮件和电话]

WooCommerce报表不执行此操作。

我找到了以下代码:

// 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;
}

它通过优惠券获取销售数量,但我想获取具有折扣的销售数量并返回[订单号,订单日期,订单状态,订单总额,用户名,电子邮件和电话]

我应该从哪里开始完成这项任务?任何曲目都值得赞赏。

0 个答案:

没有答案
相关问题