Conditional Statement to Detect if a User has a Subscription with Woocommerce Subscriptions

If you’re using the Woocommerce Subscriptions plugin, you may find it useful to have a conditional statement that would allow you to detect if the current user is a logged in subscriber with a subscription, whether that subscription is active, and what subscription level they have.

This is kind of built into the Woocommerce Subscriptions plugin, but it’s not elegant. If you want something a little more beautiful, add this to your functions.php:

function has_woocommerce_subscription($the_user_id, $the_product_id, $the_status) {
$current_user = wp_get_current_user();
if (empty($the_user_id)) {
$the_user_id = $current_user->ID;
}
if (wcs_user_has_subscription($the_user_id, $the_product_id, $the_status)) {
return true;
}
}

Now you can use this anywhere in your theme to check if the current user is a logged in subscriber with a Woocommerce Subscription attached:

if (has_woocommerce_subscription('','','')) {
// do something
}

Or if you want to check to see if the current user has an Active subscription:

if (has_woocommerce_subscription('','','active')) {
// this returns true if the user's subscription is Active
}

You can use the second parameter to check if they have a specific subscription. When you create subscriptions you do so by creating a Woocommerce product. That product will have an ID. You can find the ID by editing the product and looking in the address bar for ?post=123. Whatever the number is there in place of 123 is your ID.

So if we want to check if the current user has a Subscription with the Product ID of 10, we’d do this:

if (has_woocommerce_subscription('','10','')) {
// do something
}

Here we’ll use all of the parameters, first checking to see if a user with an ID of 4 has a subscription with a Product ID of 20 and that the subscription is Active.

if (has_woocommerce_subscription('4','20','active')) {
// This will only apply to the user with a User ID of 4, if they've purchased a subscription with a Product ID of 20, and if that subscription is currently Active
}

Would additional functions be useful? Let me know if you’ve got any thoughts in the comments and I’ll see what I can do!

Enjoy!

Up Next: How to Display Woothemes Sensei Modules and Lesson List in a Sidebar