Woocommerce Subscriptions: Check If Active User Has Subscription
In this article, we will break down the steps to check if an active user has a subscription in WooCommerce Subscriptions. WooCommerce Subscriptions is a powerful extension that allows you to sell products and services with recurring payments. By understanding how to verify the user’s subscription status, you can tailor the user experience on your website and provide exclusive access to premium content or features.
Table of Contents
- Introduction
- Preparing Your Environment
- Creating a Helper Function
- Using the Helper Function in Your Theme
- Implementing the Function with Shortcodes
- Using the Function with AJAX
- Adding a Frontend Notification
- Conclusion
Preparing Your Environment
Before we start working on the code, you need to ensure that WooCommerce and WooCommerce Subscriptions are installed and activated on your WordPress website. Also, make sure that you have a child theme enabled, as it’s essential for implementing custom functions and modifications.
Step 1: Install and activate the WooCommerce and WooCommerce Subscriptions plugins.
Step 2: Set up a child theme for your WordPress website.
Creating a Helper Function
We’ll begin by creating a helper function to check if an active user has a subscription. This function will be placed in your child theme’s functions.php
file.
Step 1: Open your child theme’s functions.php
file.
Step 2: Add the following code to the file:
function eds_has_active_subscription( $user_id ) {
// Get all the active subscriptions for the user
$active_subscriptions = wcs_get_users_subscriptions( $user_id );
// Check if there are any active subscriptions
if ( ! empty( $active_subscriptions ) ) {
foreach ( $active_subscriptions as $subscription ) {
if ( $subscription->has_status( 'active' ) ) {
return true;
}
}
}
return false;
}
This function uses the WooCommerce Subscriptions function wcs_get_users_subscriptions()
to retrieve all of the user’s subscriptions. It then checks if any of them are active.
Using the Helper Function in Your Theme
Now that we have our helper function, let’s see how to use it within your theme’s templates. You can use this function in a conditional statement to display different content based on the user’s subscription status.
Step 1: Open the template file where you want to implement the functionality (e.g., single.php
for single posts).
Step 2: Add the following code to the template file:
if ( is_user_logged_in() ) {
$user_id = get_current_user_id();
if ( eds_has_active_subscription( $user_id ) ) {
// Display content for users with an active subscription
} else {
// Display content for users without an active subscription
}
} else {
// Display content for guests (not logged in)
}
This code checks if the user is logged in and calls our eds_has_active_subscription()
function to determine their subscription status.
Implementing the Function with Shortcodes
If you want to use our helper function within the WordPress editor, you can create a custom shortcode.
Step 1: Open your child theme’s functions.php
file.
Step 2: Add the following code to the file:
function eds_has_subscription_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() ) {
$user_id = get_current_user_id();
if ( eds_has_active_subscription( $user_id ) ) {
return do_shortcode( $content );
}
}
return '';
}
add_shortcode( 'eds_has_subscription', 'eds_has_subscription_shortcode' );
Now you can use the [eds_has_subscription]
shortcode in the WordPress editor to wrap content that should only be visible to users with an active subscription.
Using the Function with AJAX
In some cases, you might want to check a user’s subscription status using AJAX. This can be useful for updating content on the page without refreshing it.
Step 1: Create a new JavaScript file in your child theme’s folder with the prefix “eds-“, such as eds-subscription-check.js
.
Step 2: Add the following jQuery code to the new file:
jQuery(document).ready(function($) {
$.ajax({
url: ajaxurl,
data: {
action: 'eds_check_subscription_status'
},
success: function( response ) {
if ( response === '1' ) {
// The user has an active subscription
} else {
// The user does not have an active subscription
}
}
});
});
Step 3: Enqueue the script in your child theme’s functions.php
file:
function eds_enqueue_scripts() {
wp_enqueue_script( 'eds-subscription-check', get_stylesheet_directory_uri() . '/eds-subscription-check.js', array( 'jquery' ), '1.0.0', true );
wp_localize_script( 'eds-subscription-check', 'ajaxurl', admin_url( 'admin-ajax.php' ) );
}
add_action( 'wp_enqueue_scripts', 'eds_enqueue_scripts' );
Step 4: Add the following code to your functions.php
file to handle the AJAX request:
function eds_check_subscription_status() {
if ( is_user_logged_in() ) {
$user_id = get_current_user_id();
if ( eds_has_active_subscription( $user_id ) ) {
echo '1';
} else {
echo '0';
}
} else {
echo '0';
}
wp_die();
}
add_action( 'wp_ajax_eds_check_subscription_status', 'eds_check_subscription_status' );
add_action( 'wp_ajax_nopriv_eds_check_subscription_status', 'eds_check_subscription_status' );
Adding a Frontend Notification
To inform users about their subscription status, you can display a notification on your website.
Step 1: Create a new eds-notification.php
file in your child theme’s folder.
Step 2: Add the following code to the new file:
<?php if ( is_user_logged_in() ) : ?>
<?php if ( eds_has_active_subscription( get_current_user_id() ) ) : ?>
<div class="eds-notification eds-subscription-active">
<p>You have an active subscription.</p>
</div>
<?php else : ?>
<div class="eds-notification eds-subscription-inactive">
<p>You do not have an active subscription.</p>
</div>
<?php endif; ?>
<?php endif; ?>
Step 3: Include the eds-notification.php
file in your theme’s template files (e.g., header.php
) using the following code:
<?php get_template_part( 'eds-notification' ); ?>
Conclusion
Thank you for reading this article on how to check if an active user has a subscription in WooCommerce Subscriptions. By following these steps, you can create a more personalized experience for your users and control access to premium content or features based on their subscription status.
Remember to adapt and customize the code examples provided to suit your specific use case and design. With a better understanding of how to work with WooCommerce Subscriptions, you can continue to optimize your website and offer a seamless user experience for your subscribers.
If you have any questions or need further assistance, don’t hesitate to consult the official WooCommerce Subscriptions documentation or reach out to the WooCommerce support team. Good luck with your project, and happy coding!