Detect the current user has an active subscription in Woocommerce.
The question comes in mind when a user with “subscriber” role with an active subscription login and try to post a content every time he / she has to choose a package even he has an active subscription.
The same can be solved by Using WooCommerce Subscriptions dedicated conditional function. Woocommerce works like magic with its extensions and gave us ability to do with inbuilt functions.
The same can be solved by Using WooCommerce Subscriptions dedicated conditional function. Woocommerce works like magic with its extensions and gave us ability to do with inbuilt functions.
So no more hesitation around the code. You don’t need to build a program to achieve that. Please refer to the following code
Detect the current user has an active subscription in Woocommerce
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; }
Or
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>'; }
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>'; }
WooCommerce Subscriptions allows you to introduce a variety of subscriptions for physical or virtual products and services. Create product-of-the-month clubs, weekly service subscriptions or even yearly software billing packages. Add sign-up fees, offer free trials, or set expiration periods.
A subscription-based model will allow you to capture more residual revenue — and all you have to do is ship the orders.
Why Use WooCommerce Subscriptions?
- Multiple billing schedules available to suit your store’s needs
- Integration with over 25 payment gateways for automatic recurring payments
- Supports manual renewal payments through any WooCommerce payment gateway, along with automatic email invoices and receipts
- Supports automatic rebilling on failed subscription payments, so you never lose revenue
- Give subscribers the ability to manage their own plan, including upgrading or downgrading, without needing to wait on you for help
- Built-in renewal notifications and automatic emails let you — and your customers — know when subscription payments have been processed, so there are never any surprises
- Detailed reports allow you to keep track of recurring revenue, number of active subscribers, and more
( Read More )
Read More Posts
- How to get values and parameters from url with jquery in cordova with example?
- How to get multiple values from ajax in wordpress?
- How to create stylist table in html and css?
- How to create print button function?
- How to redirect user on different page accounding to user (Customer, editor, admin, subscriber and etc) on wordpress dashboard?