我需要一个查询,向我回复所有已批准付款订单的用户。
我很难找到数据库存储的位置。我发现只有post_status'wc-complete',但我不认为这是正确的信息。
SELECT a.post_status, b.meta_value FROM wp_posts a, wp_postmeta b
WHERE a.ID = b.post_id
AND a.post_type = 'shop_order'
AND a.post_status = 'wc-completed'
答案 0 :(得分:3)
对付款订单使用“已完成”订单状态是正确的
这是一个带有SQL查询的自定义函数,它将输出用户ID的格式化数组及其已完成的订单(已付款/已接受):
function completed_orders_ids_by_costumer_id(){
global $wpdb;
$query = $wpdb->get_results("
SELECT pm.meta_value AS user_id, pm.post_id AS order_id
FROM {$wpdb->prefix}postmeta AS pm
LEFT JOIN {$wpdb->prefix}posts AS p
ON pm.post_id = p.ID
WHERE p.post_type = 'shop_order'
AND p.post_status = 'wc-completed'
AND pm.meta_key = '_customer_user'
ORDER BY pm.meta_value ASC, pm.post_id DESC
");
// We format the array by user ID
foreach($query as $result)
$results[$result->user_id][] = $result->order_id;
return $results;
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
经过测试和工作。
示例用法 仅用于测试以查看原始输出数据
echo '<pre>'; print_r(completed_orders_ids_by_costumer_id()); echo '</pre>';
你会得到类似的东西:
Array
(
[9] => Array
(
[0] => 505
[0] => 497
)
[12] => Array
(
[0] => 626
[1] => 584
[2] => 483
)
[15] => Array
(
[0] => 614
[1] => 598
)
[17] => Array
(
[0] => 634
)
… / … and so on …
)
现在在
wp_postmeta
表格中,您还可以使用与付费订单相关的其他元键:
•'_paid_date'
•'_date_paid'
•'_date_completed'
•'_completed_date'
但定位“已完成”的订单状态还不错
使用其他列出的元键,您可以进行SQL查询(可以替换我们在函数中的第一个查询):
$query = $wpdb->get_results("
SELECT DISTINCT pm.meta_value AS user_id, pm.post_id AS order_id
FROM {$wpdb->prefix}postmeta AS pm
LEFT JOIN {$wpdb->prefix}posts AS p
ON pm.post_id = p.ID
LEFT JOIN {$wpdb->prefix}postmeta AS pm2
ON p.ID = pm2.post_id
WHERE p.post_type = 'shop_order'
AND pm.meta_key = '_customer_user'
AND pm2.meta_key IN ( '_paid_date', '_paid_date', '_date_completed', '_completed_date' )
AND pm2.meta_value != ''
ORDER BY pm.meta_value ASC, pm.post_id DESC
");
经过测试并且也有效......