The Subscriptions module is one of the most popular modules in our UltimateWoo plugin for WooCommerce. It’s a very powerful tool for content creators or any product creator to sell their products or services with recurring billing.
Sometimes, website owners need to limit some content to customers with active subscriptions. For example, let’s say you sell Product A and provide buyer’s of Product A with a supplementary support forum. You’ve set up your Subscription product in WooCommerce and it includes a one-time fee to cover the cost of the actual product. The recurring fee is for the support forum. When a customer is charged, his access to the forum will continue. When the customer cancels his subscription and is no longer paying for access, he should not be able to access the forum. Pretty straightforward.
Now how do we go about doing this using WooCommerce Subscriptions? It’s also pretty straightforward. Let’s start with a code example.
This code uses the wcs_user_has_subscription()
function to determine if the current user has an active subscription. The function accepts three parameters: $user_id (any user’s ID; current user if left empty), $product_id (any subscription product ID; any product if empty), and $status (any status if empty). For our purposes, we want to know if the current user has an active subscription to any product. If yes, then allow access to the current forum post type by not redirecting. If the user does not have an active subscription, then redirect to the page with an ID of 2626, which in our example is a signup page.
Do not use this code in a live environment because it is for demo purposes only to show how the wcs_user_has_subscription()
function works. You can use this function however you would like to determine if any given user has any type of subscription to any product in your WooCommerce store.
What’s wrong with using this code in a live environment?
Hi, Babak. It’s not the you can’t use it in a live environment. You’d need to customize it to fit your requirements. For example, `is_singular(‘forum’)` and `2626` may not apply to your use, so those would need changed, or the code wouldn’t work as you’d expect.