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 中的相应组。
这在大多数情况下都能正常工作。
但有时它不是自动化的。例如,在购买会员资格时,80% 的用户已分配到特定的 Discourse 组,但另外 20% 的用户没有。因此,我想为所有用户实现自动化。
有什么建议可以让我实现完全自动化吗?
嘿 ![]()
你能帮我确认一下这些事情吗:
- 你正在使用 Discourse 作为你的 Discourse Connect 提供商(人们使用 Discourse 登录表单登录到 WordPress)
- 你正在 WordPress 中使用 WP Discourse 插件
- 你正在 WordPress 中使用我在此主题第一篇帖子中发布的代码
对吗?
您的第二个选项是正确的,我正在使用 discourse 插件。正如我提到的,我的用户在购买特定会员资格后会自动分配到特定组。
一切正常。只是几个月或更长时间后(偶尔)会出现这个问题,即一个随机用户在购买会员资格后未被分配到特定组。
我也想摆脱这种偶尔发生的问题。
如果只有第二个选项是正确的,那么这些说明将无法满足您的需求。这三者都需要为真。听起来您已经有一个同步在工作。为了让我理解,您是否\n\n1. 安装了 WP Discourse 插件;并且\n\n2. 编写了代码来同步您的 WooCommerce 群组(例如 如下所示)\u003c-- 您是否添加了此项?您能否分享您添加的内容?
不,我认为该插件完成了所有自动化,我不记得放置了任何集成代码。
也许我的插件是 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 但也没成功。