woocommerce用户SQL查询2周内没有订单



我在获取过去2周内没有在woocommerce订购的用户数据时遇到了麻烦。这段代码只拉一个人的数据,我需要一个sql查询轮询所有用户

function has_bought($user_id) {
// Get all customer orders
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key'    => '_customer_user',
'meta_value'  => $user_id,
'post_type'   => 'shop_order', // WC orders post type
'post_status' => 'wc-completed' // Only orders with status "completed"
) );
// return "true" when customer has already one order
return count( $customer_orders ) > 0 ? true : false;
}

可以使用date_query。试试下面的代码:

function has_bought($user_id) {
// Get all customer orders
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key'    => '_customer_user',
'meta_value'  => $user_id,
'post_type'   => 'shop_order', // WC orders post type
'post_status' => 'wc-completed', // Only orders with status "completed"
'date_query' => array(
'after' => date('Y-m-d', strtotime('-14 days')) 
)
) );
// return "true" when customer has already one order
return count( $customer_orders ) > 0 ? true : false;
}

相关内容

最新更新