繁体   English   中英

在 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 查询会轻得多。

因为您不必重新发明热水,我在 Woocommerce 管理员优惠券编辑页面答案代码的显示自定义数据中找到了完美的解决方案。

新答案:

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

结果优惠券


相关:在 WooCommerce 管理员优惠券列表中添加一个带有作者姓名的新列

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM