I am having trouble getting the data of users who did not order in the last 2 weeks in woocommerce. this code is pulling only one person data, I need a sql query to poll all users
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;
}
CodePudding user response:
You can use date_query. try the below code.
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;
}