php - 在 WooCommerce 管理员优惠券列表的新列中显示优惠券产生的总销售额

标签 php wordpress woocommerce backend coupon

我正在使用以下代码 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 过滤器 Hook ,它允许您在 WooCommerce 管理员优惠券列表上创建一个新列。

然后,manage_shop_coupon_posts_custom_column 操作 Hook 允许您将内容添加到新列。

因此,要在 WooCommerce 管理员优惠券列表的新列中显示优惠券产生的总销售额,请使用:

编辑:

在我发布我的第一个答案后,我意识到你得到的订单越多,就越重……而直接的 SQL 查询会轻得多。

因为您不必重新发明热水,所以我在 Display custom data on Woocommerce admin coupon edit pages 中找到了答案代码完美的解决方案。

新答案:

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

result_coupon


相关:Add a new column with author name to WooCommerce admin coupon list

关于php - 在 WooCommerce 管理员优惠券列表的新列中显示优惠券产生的总销售额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67883874/

相关文章:

php - 从 PHP 和 Timber/Twig 中的高级自定义字段对转发器字段进行排序

javascript - 在 Woocommerce 中触发 check_variations 事件

wordpress - Woocommerce:更改 "# in stock"文本

wordpress - 无法在 LOCALHOST 上安装 woocommerce 插件

ios - 如何让 WhatsApp for iOS 获取我网站 og 标签中设置的预览图像?

php - 如何选择mysql结果?

php - 如何删除自豪地由 Wordpress 条目 (RSS) 和评论 (RSS) 提供支持的网站名称

javascript - 显式返回转义的 JSON 响应与仅在 Laravel/PHP 中返回数组

PHP:转义非字母数字字符

javascript - jQuery 无法在 Wordpress 中工作 - 默认情况下工作