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.)
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: