Detect the current user has an active subscription in Woocommerce.
// When a $user_id is not specified, get the current user Id
if( null == $user_id && is_user_logged_in() )
$user_id = get_current_user_id();
// User not logged in we return false
if( $user_id == 0 )
return false;
// Get all active subscriptions for a user ID
$active_subscriptions = get_posts( array(
'numberposts' => 1, // Only one is enough
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_subscription', // Subscription post type
'post_status' => 'wc-active', // Active subscription
'fields' => 'ids', // return only IDs (instead of complete post objects)
) );
return sizeof($active_subscriptions) == 0 ? false : true;
}
Or
// do something … here goes your code
// Example of displaying something
echo '<p>I have active subscription</p>';
}
Or
if( has_active_subscription(26) ){ // Defined User ID has an active subscription
// do something … here goes your code
// Example of displaying something
echo '<p>User ID "26" have an active subscription</p>';
}
Comments
Post a Comment