我正在开发一个自定义 WordPress 插件,以使用 SQL 查询在管理面板中显示 WooCommerce 订单统计信息。但是,该插件没有显示正确的数据。
问题:
该插件应该显示已完成和正在处理的订单总数,以及它们的总金额和已取消订单的总数及其金额。但是,显示的统计数据不正确或根本不显示。
预期输出:
订单总数:[预计数量]
总订单金额:[预计金额]
取消订单总数:[预计数量]
已取消订单的总金额:[预计金额]
<?php
/*
Plugin Name: WooCommerce Order Stats
Description: Displays order statistics for WooCommerce in the WordPress admin panel.
Version: 1.0
Author: Your Name
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
// Function to display WooCommerce order statistics
function display_woocommerce_order_stats_admin_notice() {
global $wpdb;
$table_prefix = $wpdb->prefix;
$orders_table = "{$table_prefix}wc_orders"; // WooCommerce orders table name
// Query total number of completed orders
$total_completed_orders = $wpdb->get_var("
SELECT COUNT(ID)
FROM {$orders_table}
WHERE post_status = 'wc-completed'
");
// Query total amount for completed orders
$total_completed_amount = $wpdb->get_var("
SELECT SUM(meta.meta_value)
FROM {$orders_table} AS orders
INNER JOIN {$table_prefix}postmeta AS meta ON orders.ID = meta.post_id
WHERE orders.post_status = 'wc-completed'
AND meta.meta_key = '_order_total'
");
// Query total number of processing orders
$total_processing_orders = $wpdb->get_var("
SELECT COUNT(ID)
FROM {$orders_table}
WHERE post_status = 'wc-processing'
");
// Query total amount for processing orders
$total_processing_amount = $wpdb->get_var("
SELECT SUM(meta.meta_value)
FROM {$orders_table} AS orders
INNER JOIN {$table_prefix}postmeta AS meta ON orders.ID = meta.post_id
WHERE orders.post_status = 'wc-processing'
AND meta.meta_key = '_order_total'
");
// Query total number of cancelled orders
$total_cancelled_orders = $wpdb->get_var("
SELECT COUNT(ID)
FROM {$orders_table}
WHERE post_status = 'wc-cancelled'
");
// Query total amount for cancelled orders
$total_cancelled_amount = $wpdb->get_var("
SELECT SUM(meta.meta_value)
FROM {$orders_table} AS orders
INNER JOIN {$table_prefix}postmeta AS meta ON orders.ID = meta.post_id
WHERE orders.post_status = 'wc-cancelled'
AND meta.meta_key = '_order_total'
");
// Calculate total orders and total amount
$total_orders = $total_completed_orders + $total_processing_orders;
$total_order_amount = $total_completed_amount + $total_processing_amount;
// Output the statistics and table name as an admin notice
echo '<div class="notice notice-info">';
echo '<p><strong>WooCommerce Order Statistics (Table: ' . $orders_table . '):</strong></p>';
echo '<ul>';
echo '<li>Total Number of Orders: ' . $total_orders . '</li>';
echo '<li>Total Amount for Total Orders: ' . wc_price($total_order_amount) . '</li>';
echo '<li>Total Cancelled Orders: ' . $total_cancelled_orders . '</li>';
echo '<li>Total Amount for Cancelled Orders: ' . wc_price($total_cancelled_amount) . '</li>';
echo '</ul>';
echo '</div>';
}
// Hook into the admin menu
add_action('admin_notices', 'display_woocommerce_order_stats_admin_notice');
您的 SQL 查询有一些错误:
wc_orders
表中的索引列是id
但不是ID
,JOIN
另一个表来获取订单总金额,因为您可以使用wc_orders
表total_amount
列,所以将你的函数替换为:
function display_woocommerce_order_stats_admin_notice() {
global $wpdb;
// Query total number of completed orders
$total_paid_orders = $wpdb->get_var("
SELECT COUNT(id)
FROM {$wpdb->prefix}wc_orders
WHERE status IN ('wc-completed', 'wc-processing')
");
// Query total amount for completed orders
$total_paid_orders_amount = $wpdb->get_var("
SELECT SUM(total_amount)
FROM {$wpdb->prefix}wc_orders
WHERE status IN ('wc-completed', 'wc-processing')
");
// Query total number of cancelled orders
$total_cancelled_orders = $wpdb->get_var("
SELECT COUNT(id)
FROM {$wpdb->prefix}wc_orders
WHERE status = 'wc-cancelled'
");
// Query total amount for cancelled orders
$total_cancelled_orders_amount = $wpdb->get_var("
SELECT SUM(total_amount)
FROM {$wpdb->prefix}wc_orders
WHERE status = 'wc-cancelled'
");
// Output the statistics and table name as an admin notice
echo '<div class="notice notice-info">';
echo '<p><strong>WooCommerce Order Statistics (Table: wc_order):</strong></p>';
echo '<ul>';
echo '<li>Total Number of Orders: ' . $total_paid_orders . '</li>';
echo '<li>Total Amount for Total Orders: ' . wc_price($total_paid_orders_amount) . '</li>';
echo '<li>Total Cancelled Orders: ' . $total_cancelled_orders . '</li>';
echo '<li>Total Amount for Cancelled Orders: ' . wc_price($total_cancelled_orders_amount) . '</li>';
echo '</ul>';
echo '</div>';
}
应该会更好用