# Please Check Code For Assigning Group to WooCommerce Memberships Members

**URL:** <https://meta.discourse.org/t/please-check-code-for-assigning-group-to-woocommerce-memberships-members/113013>\
**Category:** Support\
**Created:** [March 30, 2019, 4:20pm UTC](https://meta.discourse.org/t/please-check-code-for-assigning-group-to-woocommerce-memberships-members/113013 "2019-03-30T16:20:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bernstein](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bernstein/32/304070_2.png) [@Bernstein](https://meta.discourse.org/u/Bernstein)\
**Post date:** [March 30, 2019, 4:20pm UTC](https://meta.discourse.org/t/please-check-code-for-assigning-group-to-woocommerce-memberships-members/113013/1 "2019-03-30T16:20:52Z")

</div>

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:

```plaintext
/**
 * 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.)

---

<div class="post-metadata">

**Author:** ![Bernstein](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bernstein/32/304070_2.png) [@Bernstein](https://meta.discourse.org/u/Bernstein)\
**Post date:** [April 3, 2019, 12:33pm UTC](https://meta.discourse.org/t/please-check-code-for-assigning-group-to-woocommerce-memberships-members/113013/2 "2019-04-03T12:33:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Corsterix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/corsterix/32/135849_2.png) [@Corsterix](https://meta.discourse.org/u/Corsterix)\
**Post date:** [April 3, 2019, 12:55pm UTC](https://meta.discourse.org/t/please-check-code-for-assigning-group-to-woocommerce-memberships-members/113013/3 "2019-04-03T12:55:48Z")

</div>

Have a look at this:

[https://github.com/scossar/wp-discourse-woocommerce-support](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;
    }
}

```

---

<div class="post-metadata">

**Author:** ![Bernstein](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bernstein/32/304070_2.png) [@Bernstein](https://meta.discourse.org/u/Bernstein)\
**Post date:** [April 3, 2019, 2:08pm UTC](https://meta.discourse.org/t/please-check-code-for-assigning-group-to-woocommerce-memberships-members/113013/4 "2019-04-03T14:08:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [April 3, 2019, 4:12pm UTC](https://meta.discourse.org/t/please-check-code-for-assigning-group-to-woocommerce-memberships-members/113013/5 "2019-04-03T16:12:18Z")

</div>

> [@Bernstein](#):
>
> I wonder if Wordpress has to be the SSO Provider instead of the Client for all of this to function?

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:

```php
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.

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [July 10, 2022, 9:54pm UTC](https://meta.discourse.org/t/please-check-code-for-assigning-group-to-woocommerce-memberships-members/113013/7 "2022-07-10T21:54:27Z")

</div>

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