If you don’t need people to get removed from the group after some period of time, that is, they buy lifetime access to the group rather than temporary/periodic access to the group, then you can hook to woocommerce_payment_complete and add them to the group when the purchase is made. You can also hook to the SSO and update their groups at login.
Thanks Jay!
Out of the gate we will not offer a lifetime access option for any of our membership groups. The nature of our “mentor marketplace” is that users will have a coach/mentor whose group they want to join. They may try it for a couple months and then cancel, and choose to sign up with a different mentor that might better suit their needs or expectations.
So we would need an active SYNC in order that when a member subscribes to a custom group in WP, then are automatically added to the respective group, in Discourse, and if anyone cancels their membership on WP, they would be automatically removed from the group, in Discourse.
If you want the latest version of this code you can find it in this repository: https://github.com/paviliondev/discourse-woocommerce.
This is used in production and continues to work as described.
Steps
- Install the code as a wordpress plugin.
- Update the group and plan ids.
- The sync will occur on each membership status change.
If someone could wikify the OP, I’ll update it with better instructions.
Hi! I bought the plugin Woocommerce memberships. It does everything you need. Then everything cleared for me. I posted here an instruction on a fully working integration that I set.
So do you install the plugin in the OP first?
I’m like you in that I’m using Wordpress as the provider for our SSO… not Discourse (as noted in this topic title and OP)
I know the OP is about Syncing groups when Discourse is the SSO provider, but would it make sense to update the topic title and include instructions for doing this sync when Wordpress is the SSO provider?
@Ed_Bobkov provided some details on how to do the sync with an inverse of the SSO provider…
So just to clarify…
It would be really great and probably helpful to others In the long run if there were two different sets of instructions in the OP…
Sync WooCommerce Memberships with Discourse groups when:
- Discourse is the SSO Provider
- Wordpress is the SSO Provider
You are wrong. I use Discourse as a SSO provider. But that doesn’t change the sense.
You can find here topics with instructions when WP is SSO provider.
The main thing is that:
- You can manage your group memberships in Wordpress, subscriptions and terms by the plugin “WooCommerce Memberships” in all cases - when WP is a SSO provider OR Discourse is a SSO provider.
- To sync Discours members and groups with Wordpress you need in all cases use this plugin: GitHub - discourse/wp-discourse: WordPress plugin that lets you use Discourse as the community engine for a WordPress blog. In its’ settings you can choose the type of integration - WP or Discourse is a SSO provider.
Also this plugin may be useful: GitHub - paviliondev/discourse-woocommerce - To do a batch syncing by cron you need to use this plugin: WP Crontrol – WordPress plugin | WordPress.org
Detailed instructions you can find in this topic (above).
That link is now a 404. Did it move?
The OP is now a wiki.
I’ve updated the OP with a link to the latest version of the code and instructions.
Ah I had forgot to make it public. Thanks.
@jord8on As Ed mentions, you should use the official Discourse Wordpress plugin if Wordpress is the SSO provider.
WooCommerceとDiscourseの連携に関して問題が発生しています。
特定のメンバーシップを購入した場合、ユーザーはDiscourseの該当グループに自動的に割り当てられるはずです。
ほとんどの場合、これは正常に機能しています。
しかし、時々自動化されません。例えば、10人のユーザーのうち8人はメンバーシップ購入時に特定のDiscourseグループに割り当てられましたが、残りの2人は割り当てられませんでした。そのため、すべてのユーザーに対して自動化したいと考えています。
これを完全に自動化する方法について、何か提案はありますか?
こんにちは ![]()
これらのことをそれぞれ確認していただけますか?
- ディスコースをディスコースコネクトプロバイダーとして使用していますか?(ユーザーはディスコースログインフォームを使用してWordPressにサインインします)
- WordPressでWP Discourseプラグインを使用していますか?
- このトピックの最初の投稿で私が投稿したコードをWordPressで使用していますか?
これで合っていますか?
2番目のオプションで合っています。Discourseプラグインを使用しており、特定のメンバーシップを購入すると、ユーザーは特定のグループに自動的に割り当てられると述べました。
すべて正常に動作しています。ただ、数ヶ月に一度(時々)、特定のメンバーシップを購入しても、ランダムなユーザーが特定のグループに割り当てられないという問題が発生します。
この時々発生する問題も解消したいです。
残念ながら、2番目のオプションだけが正しい場合、これらの手順は機能しません。3つすべてが真実である必要があります。同期は機能しているようですね。理解のために、以下のことを確認してください。
- WP Discourse プラグインがインストールされていること。
- WooCommerce グループを同期するためのコードがあること (例: こちら) ← これを追加しましたか?追加したものを共有していただけますか?
いいえ、プラグインがすべての自動化を行ったと思います。統合のためにコードを配置した記憶はありません。
私のプラグインはバージョン2.2.3で、利用可能なアップデートは2.4.1なので、古い可能性があります。
このバージョンが原因で発生しているのかもしれません。しかし、アップデート後に現在の機能に影響があったのかどうか、少し気になっています。
WooCommerceとDiscourse間でグループを同期するには、カスタムコードが必要です。WP Discourseプラグインだけでは機能しません。おそらく、ここで説明されている関数の実装があるはずです。
さらにヘルプを提供するには、まず同期がどのように行われているかを特定する必要があります(WP Discourseプラグインだけではありません)。
WordPress にコードを追加しました。@Ed_Bobkov が公開したものです。
<?php
//wp+discourse
use WPDiscourse\Utilities\Utilities as DiscourseUtilities;
const MEMBERSHIP_PLAN_DISCOURSE_GROUP = [
“112” => “41”
];
//const ACTIVE_STATUSES = array( ‘wcm-active’ );
const ACTIVE_STATUSES = array( ‘wcm-active’, ‘wcm-free_trial’ );
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/" . MEMBERSHIP_PLAN_DISCOURSE_GROUP[$membership_plan_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 && isset(MEMBERSHIP_PLAN_DISCOURSE_GROUP[$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 );
/* run_full_wc_membership_sync */
function full_wc_membership_sync() {
$allusers = get_users();
$logger = wc_get_logger();
$logger->info( sprintf('Running full_wc_membership_sync') );
foreach ( $allusers as $user ) {
$user_id = $user->id;
$membership = wc_memberships_get_user_membership($user_id);
$membership_plan_id = $membership->plan->id;
$logger->info( sprintf('Checking membership of %s', $user->user_login) );
if ($membership && isset(MEMBERSHIP_PLAN_DISCOURSE_GROUP[$membership_plan_id])) {
$membership_plan_name = $membership->plan->name;
$status = $membership->status;
$logger->info( sprintf('Updating group access of %s', $user->user_login) );
update_discourse_group_access($user_id, $membership_plan_id, $membership_plan_name, $status);
$logger->info( sprintf('Sleeping for 5 seconds') );
sleep(5);
}
}
}
add_action(‘run_full_wc_membership_sync’, ‘full_wc_membership_sync’);
また、WP Crontrol を介して run_full_wc_membership_sync を追加して実行しました。
残念ながら…アクティブなメンバーシップ(ID 112)を持つ WordPress ユーザーが Discourse グループ(ID 41)に追加されていません。何が足りないのでしょうか?
これが人々を混乱させ続けているので、WooCommerceメンバーシップの同期(WordPressまたはDiscourseのいずれかがDiscourseConnectプロバイダーであるかどうかにかかわらず)に関する完全な「ハウツー」を公開します
。まだそれについてあなたに連絡していない場合は、来週初めにここを確認してください。
ありがとうございます。調べてみました… プラグイン GitHub - paviliondev/discourse-woocommerce も試しましたが、うまくいきませんでした。