Skip to content

Instantly share code, notes, and snippets.

@Lonsdale201
Last active March 20, 2024 15:46
Show Gist options
  • Save Lonsdale201/a2818bca6dabcbfc812bbcebe059bf1d to your computer and use it in GitHub Desktop.
Save Lonsdale201/a2818bca6dabcbfc812bbcebe059bf1d to your computer and use it in GitHub Desktop.
JetEngine - Dynamic Visibility - Woo Subscriptions (current user any active sub)
// place the code in the child theme functions.php or a custom code snippets plugin.
// This conditions required the Woo Subscriptions plugin to be installed and activated on your site. This Conditions only work with
// LOGGED IN USERS
// https://woo.com/products/woocommerce-subscriptions/
// This is the code to see if the current logged in user has any subscriptions that are active.
add_action( 'jet-engine/modules/dynamic-visibility/conditions/register', function( $conditions_manager ) {
class Woo_Subscriptions_Active_Subscription extends \Jet_Engine\Modules\Dynamic_Visibility\Conditions\Base {
public function get_id() {
return 'woo-subscriptions-active-subscription';
}
public function get_name() {
return __( 'Current user any active subscriptions', 'jet-engine' );
}
public function get_group() {
return 'Woo Subscriptions';
}
public function check($args = array()) {
$user_id = get_current_user_id();
if (!$user_id) {
return false;
}
if (!function_exists('wcs_get_users_subscriptions')) {
return false;
}
$subscriptions = wcs_get_users_subscriptions($user_id);
$has_active_subscription = false;
foreach ($subscriptions as $subscription) {
if ($subscription->has_status('active')) {
$has_active_subscription = true;
break;
}
}
$type = isset($args['type']) ? $args['type'] : 'show';
return ('hide' === $type) ? !$has_active_subscription : $has_active_subscription;
}
public function is_for_fields() {
return false;
}
public function need_value_detect() {
return false;
}
}
$conditions_manager->register_condition( new Woo_Subscriptions_Active_Subscription() );
} );
@Lonsdale201
Copy link
Author

fixed the missing args

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment