Detect if current user has an active subscription

I am developing a WordPress site with WooCommerce. I also use WC Paid Listings and WooCommerce Subscription plugins for my work.

The problem is that a user with the subscriber role with an active subscription logs in and tries to publish content every time he / she has to choose a package, even if he has an active subscription.

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

Thank you

+7
source share
2 answers

Updated (2019)

The following user-defined conditional functions have the optional argument $user_id (defined by user_id) and return true if the current user (or specific user) has active subscriptions.

So this can be done now using 3 different methods (which do the same):

1) Using a WooCommerce subscription for the wcs_user_has_subscription() conditional function:

 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 with a much easier SQL query (added in 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; } 

The code is placed in the function.php file of your active child theme (or theme), as well as in any plug-in file.


3) Original improved 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; } 

The code is placed in the function.php file of your active child theme (or theme), as well as in any plug-in file.


Usage Update:

1) USE for 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) USE for a specific 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 has been verified and works.


Related answers:

+9
source

Use wcs_user_has_subscription()

 $has_sub = wcs_user_has_subscription( '', '', 'active' ); if ( $has_sub) { // User have active subscription } 
+3
source

Source: https://habr.com/ru/post/1264599/


All Articles