我正在尝试显示付款状态为“ ppec-paypal”的某些状态下所有订单的总价值。
我尝试使用AND函数按meta.meta_value进行过滤
add_shortcode( 'show_total_pp', 'show_total_pp_shortcode' );
function show_total_pp_shortcode() {
global $wpdb;
$order_totalpp = 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 meta.meta_value = 'ppec_paypal'
AND posts.post_status IN ( '" . implode( "','", array( 'wc-processing','wc-on-hold' ) ) . "' )
" ) );
return absint( $order_totalpp->total_sales);
echo $order_totalpp;
}
我有下面的代码字符串用于仅基于状态的订单,但是当我尝试通过添加以下行来过滤付款方式时: “ AND meta.meta_value _payment_method ='ppec_paypal'” 它不起作用,得到一个“ 0”值。 我猜我的代码中有语法错误。
答案 0 :(得分:1)
对于左联接,您不应在where子句中使用与左联接表相关的列(这种方式可作为内部联接) 但您应将此条件添加到相关的ON子句
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
AND meta.meta_key = '_order_total'
AND meta.meta_value = 'ppec_paypal'
WHERE posts.post_type = 'shop_order'
AND posts.post_status IN ( '" . implode( "','", array( 'wc-processing','wc-on-hold' ) ) . "' )