Detecting if the current user has an active subscription

╄→尐↘猪︶ㄣ 提交于 2019-12-04 04:28:56

Updated (2019)

  • New conditional function using WooCommerce Subscriptions wcs_user_has_subscription().
  • New conditional function using a much lighter code version (SQL query).
  • Original enhanced conditional function based on an improved WP_Query.

The following custom conditional functions have an optional argument $user_id (a defined user_id) and will return true when the current user (or a defined user) has active subscriptions.

So this can be done now using 3 different ways (that do the same thing):

1) Using WooCommerce Subscriptions dedicated conditional function wcs_user_has_subscription():

function has_active_subscription( $user_id='' ) {
    // When a $user_id is not specified, get the current user Id
    if( '' == $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;

    return wcs_user_has_subscription( $user_id, '', 'active' );
}

2) The same thing with a much lighter SQL query (added on March 2019):

function has_active_subscription( $user_id=null ) {
    // 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;

    global $wpdb;

    // Get all active subscriptions count for a user ID
    $count_subscriptions = $wpdb->get_var( "
        SELECT count(p.ID)
        FROM {$wpdb->prefix}posts as p
        JOIN {$wpdb->prefix}postmeta as pm 
            ON p.ID = pm.post_id
        WHERE p.post_type = 'shop_subscription' 
        AND p.post_status = 'wc-active'
        AND pm.meta_key = '_customer_user' 
        AND pm.meta_value > 0
        AND pm.meta_value = '$user_id'
    " );

    return $count_subscriptions == 0 ? false : true;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.


3) The original enhanced code, that will also do the same:

function has_active_subscription( $user_id=null ) {
    // 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;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.


Update of usage:

1) USAGE for the current user:

if( has_active_subscription() ){ // Current user has an active subscription 
    // do something … here goes your code

    // Example of displaying something
    echo '<p>I have active subscription</p>';
}

2) USAGE for a defined user ID:

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>';
}

This code is tested and it works


Related answers:

phpsmashcode

Use wcs_user_has_subscription()

$has_sub = wcs_user_has_subscription( '', '', 'active' );

if ( $has_sub) {
    // User have active subscription
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!