如何通过邮政编码添加"销售"到Woocommerce报道

时间:2017-08-23 13:36:45

标签: php wordpress woocommerce

我已将以下代码添加到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";
}

而不是"函数callback_postal_code"中的回声功能,我需要这个脚本来过滤用于下订单并相应显示它们的实际邮政编码。

我正在寻找正确的触发器来完成此功能。我还没设法找到它。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可以创建自定义功能,如下所示:

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

}