# Sync WooCommerce Memberships with Discourse groups

**URL:** https://meta.discourse.org/t/sync-woocommerce-memberships-with-discourse-groups/118485
**Category:** Extras
**Tags:** pavilion
**Created:** [May 23, 2019, 7:13am UTC](https://meta.discourse.org/t/sync-woocommerce-memberships-with-discourse-groups/118485 "2019-05-23T07:13:06Z")
**Posts on this page:** 1
**Showing post:** 11

<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: [May 14, 2020, 12:06am UTC](https://meta.discourse.org/t/sync-woocommerce-memberships-with-discourse-groups/118485/11 "2020-05-14T00:06:55Z")

</div>

Here is Angus’s code, updated to use header based authentication. I have not made any other changes to it. I have also not tested the code. I won’t update the OP until someone gets a chance to test it.

```php
use WPDiscourse\Utilities\Utilities as DiscourseUtilities;

const MEMBERSHIP_PLAN_ID = 35;
const DISCOURSE_GROUP_ID = 41;
const ACTIVE_STATUSES = array( 'wcm-active' );

function update_discourse_group_access( $user_id, $membership_plan_id, $membership_plan_name, $status ) {
	$options = DiscourseUtilities::get_options();
	$base_url = $options['url'];
	$api_key = $options['api-key'];
	$api_username = $options['publish-username'];

	if ( empty( $base_url ) || empty( $api_key ) || empty( $api_username ) ) {
		return new \WP_Error( 'discourse_configuration_error', 'The WP Discourse plugin has not been properly configured.' );
	}

	$user_info = get_userdata( $user_id );
	$user_email = $user_info->user_email;
	$logger = wc_get_logger();

	$logger->info( sprintf( '%s membership of %s changed to %s', $user_email, $membership_plan_name, $status ) );

	if ( in_array( $status, ACTIVE_STATUSES ) ) {
		$action = 'PUT';
	} else {
		$action = 'DELETE';
	}

	$external_url = esc_url_raw( $base_url . "/groups/" . DISCOURSE_GROUP_ID . "/members" );

	$logger->info( sprintf( 'Sending %s request to %s with %s', $action, $external_url, $user_email ) );

	$response = wp_remote_request( $external_url,
		array(
			'method' => $action,
			'headers' => array(
				'Api-Key' => sanitize_key( $api_key ),
				'Api-Username' => sanitize_text_field( $api_username ),
			),
			'body' => array( 'user_emails' => $user_email ),
		)
	);

	$logger->info( sprintf( 'Response from Discourse: %s %s',
		wp_remote_retrieve_response_code( $response ),
		wp_remote_retrieve_response_message( $response ) ) );

	if ( ! DiscourseUtilities::validate( $response ) ) {

		return new \WP_Error( 'discourse_response_error', 'There has been an error in retrieving the user data from Discourse.' );
	}
}

function handle_wc_membership_saved( $membership_plan, $args ) {
	$logger = wc_get_logger();

	$logger->info( sprintf( 'Running handle_wc_membership_saved %s, %s, %s', $args['user_id'], $args['user_membership_id'], $args['is_update'] ) );

	$user_id = $args['user_id'];
	$membership = wc_memberships_get_user_membership( $args['user_membership_id'] );
	$membership_plan_id = $membership->plan->id;

	if ( $membership && $membership_plan_id == MEMBERSHIP_PLAN_ID ) {
		$membership_plan_name = $membership_plan->name;
		$status = $membership->status;
		update_discourse_group_access( $user_id, $membership_plan_id, $membership_plan_name, $status );
	}
}
add_action( 'wc_memberships_user_membership_saved', 'handle_wc_membership_saved', 10, 2 );

```

The code is setting a `$discourse_user_id` variable, but that variable is not being used anywhere. It could probably be removed from the code.

> [@Ed\_Bobkov](#):
>
> I installed a WP plugin ‘WooCommerce Groups’: [Groups WooCommerce Archives - WooCommerce Docs](https://docs.woocommerce.com/documentation/plugins/woocommerce/woocommerce-extensions/groups-woocommerce/) . It’s similar to WooCommerce Memberships but with less functions. I think all core function are the same.

I’m not so sure about that. The code hooks into the `wc_memberships_user_membership_saved` action hook. It seems likely that that hook is added by the WooCommerce Memberships plugin. There is probably a similar hook available in the WooCommerce Groups plugin, but it is unlikely to have the same name and parameters.

---

_[View the full topic](https://meta.discourse.org/t/sync-woocommerce-memberships-with-discourse-groups/118485)._
