Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PhouvanhKCSV/895a0f0ef72f07df1c94fd35d030dd06 to your computer and use it in GitHub Desktop.
Save PhouvanhKCSV/895a0f0ef72f07df1c94fd35d030dd06 to your computer and use it in GitHub Desktop.
Check if the current user has an active WooCommerce subscription
<?php
/**
* Check if the current user has an active subscription.
* Redirect the user if no active subscription and the current post is a singule forum.
* This code should not be copied and pasted as is. It is only to demonstrate the wcs_user_has_subscription() function.
*
* wcs_user_has_subscription( $user_id = 0, $product_id = '', $status = 'any' )
* @param int (optional) The ID of a user in the store. If left empty, the current user's ID will be used.
* @param int (optional) The ID of a product in the store. If left empty, the function will see if the user has any subscription.
* @param string (optional) A valid subscription status. If left empty, the function will see if the user has a subscription of any status.
*/
add_action( 'template_redirect', 'ultimatewoo_check_user_subscriptions' );
function ultimatewoo_check_user_subscriptions() {
$has_sub = wcs_user_has_subscription( '', '', 'active' );
if ( ! $has_sub && is_singular( 'forum' ) ) {
wp_redirect( get_permalink( 2626 ) );
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment