简体   繁体   English

如何在Woocommerce报表中添加“按邮政编码销售”

[英]How to add “Sales by postal code” to Woocommerce reports

I have added the following code to functions.php to add a "Sales by postal code" tab to the Woocommerce sales reports: 我已将以下代码添加到functions.php中,以向Woocommerce销售报告中添加“按邮政编码销售”标签:

function filter_woocommerce_admin_reports( $reports ) { 

$array = array(
    'salse_by_post_code' => array(
        'title' => 'Sales by postal code',
        'description' => '',
        'hide_title' => 1,
        'callback' => 'callback_postal_code'
    )
);

$reports['orders']['reports'] = array_merge($reports['orders']['reports'],$array);

return $reports; 
}

add_filter( 'woocommerce_admin_reports', 'filter_woocommerce_admin_reports', 10, 1 ); 

function callback_postal_code() {
    echo "Add Your Code here";
}

Instead of the echo feature in the "function callback_postal_code", I need this script to filter the actual postal codes that have been used to place orders and display them accordingly. 代替“函数callback_postal_code”中的echo功能,我需要此脚本来过滤用于下订单的实际邮政编码并相应地显示它们。

I'm looking for the correct trigger to complete this function. 我正在寻找正确的触发器来完成此功能。 I haven't managed to find it yet. 我还没有找到它。 Any advice? 有什么建议吗?

You can create custom function as below: 您可以如下创建自定义函数:

function my_custom_get_total_sales() {

global $wpdb;

$order_totals = apply_filters( 'woocommerce_reports_sales_overview_order_totals', $wpdb->get_row( "

SELECT SUM(meta.meta_value) AS total_sales, COUNT(posts.ID) AS total_orders FROM {$wpdb->posts} AS posts

LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id

WHERE meta.meta_key = '_order_total'

AND posts.post_type = 'shop_order'

AND posts._shipping_postcode='<your Shipping Code>'

AND posts.post_status IN ( '" . implode( "','", array( 'wc-completed', 'wc-processing', 'wc-on-hold' ) ) . "' )

" ) );

return absint( $order_totals->total_sales);

}

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

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