Search code examples
phpwordpresswoocommerceorderswoocommerce-subscriptions

Detecting if the current user has an active subscription


I'm developing a website in WordPress with WooCommerce. I'm using additionally WC Paid Listings and WooCommerce Subscriptions plugins to handle my work.

The problem is when a user with "subscriber" role with an active subscription login tries to post content every time he/she has to choose a package even if he has an active subscription.

Is there anyone with an idea of how to detect if the user has an active subscription, if it returns true then the step choosing package is skipped?


Solution

  • 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: