Please Check Code For Assigning Group to WooCommerce Memberships Members

I’m using code I found on GitHub but it isn’t working and I’m not sure what else I need to be doing. I want to check a users WooCommerce Memberships level in Wordpress and grant those active users access to a certain Discourse Group. I have the Wordpress Discourse plugin installed. This is the code I’m using:

/**
 * Add user to `Members` group if active WooCommerce Memberships plan is found.
 *
 * @params array $params wpdc sso data
 * @return array $params wpdc sso data including `Members` group status
 */
function wpdc_wc_memberships_add_to_dc_group( $params ) {
	// Bail if Memberships isn't active
	if ( ! function_exists( 'wc_memberships' ) ) {
		return $params;
	}

	// Get any active user memberships (requires Memberships 1.4+)
	$user_id = get_current_user_id();
	$args = array( 
		'status' => array( 'active', 'complimentary', 'pending', 'free_trial' ),
	);
	
	$active_memberships = wc_memberships_get_user_memberships( $user_id, $args );
	// Check for active memberships to enable `Members` access
	if ( ! empty( $active_memberships ) ) {
		// Add to `Members` group on Discourse
		$params['add_groups'] = 'Members';
	} else {
		// Remove from `Members` group on Discourse
        $params['remove_groups'] = 'Members';
	}
	return $params;
}
add_filter( 'wpdc_sso_params', 'wpdc_wc_memberships_add_to_dc_group' );

I changed the filler “Members” group name to my specific group name but so far, no go. Are there other requirements necessary? Does WP have to be the SSO Provider (my Discourse install is currently the provider.)

Hey @simon, @Angus mentioned that you might have an idea what’s going on here. Any guidance would be greatly appreciated.

Have a look at this:

https://github.com/scossar/wp-discourse-woocommerce-support

and this:

//check membership for forum login
add_action( 'wpdc_sso_provider_before_sso_redirect', 'wpdc_custom_check_user_membership', 99, 99 );
function wpdc_custom_check_user_membership( $user_id, $user ) {
    
    $args = array( 
        'status' => array( 'active', 'complimentary' ),
    );
    
    $memberships = wc_memberships_get_user_active_memberships($user_id, $args);
    
    if ( empty( $memberships ) /* Some condition that returns true if the user doesn't meet the membership requirement */ ) {
        
        wp_safe_redirect( 'https://abc.com' );
        exit;
    }
}
1 Like

Thanks for sharing these. I went this route, adding all of this, but it’s not working and I wonder if Wordpress has to be the SSO Provider instead of the Client for all of this to function? Not sure what else to try.

Yes, the code you have posted requires WordPress to be the SSO provider. The code at the end of it is adding parameters to the SSO payload that is sent from WordPress to Discourse:

add_filter( 'wpdc_sso_params', 'wpdc_wc_memberships_add_to_dc_group' );

Unless you are using WordPress as the SSO provider, the only way to automatically add uses to Discourse groups is to use the Discourse API.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.