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.
هذا يعمل بشكل جيد في معظم الحالات.
ولكن في بعض الأحيان، لا يتم ذلك تلقائيًا. على سبيل المثال، تم تعيين 8 من أصل 10 مستخدمين إلى مجموعة Discourse محددة عند شراء العضوية، ولكن لم يتم تعيين المستخدمين الآخرين. لذلك أريد جعله تلقائيًا لجميع المستخدمين.
هل هناك أي اقتراح حول كيفية جعله تلقائيًا بالكامل؟
أهلاً ![]()
هل يمكنك تأكيد كل هذه الأمور لي:
- هل تستخدم Discourse كمزود Discourse Connect الخاص بك (يسجل الأشخاص الدخول باستخدام نموذج تسجيل الدخول الخاص بـ Discourse لتسجيل الدخول إلى Wordpress)
- هل تستخدم إضافة WP Discourse في Wordpress
- هل تستخدم الكود الذي نشرته في المنشور الأول لهذا الموضوع في Wordpress
هل هذا صحيح؟
الخيار الثاني الخاص بك صحيح، أنا أستخدم إضافة discourse، وكما ذكرت، يتم تعيين المستخدمين تلقائيًا إلى مجموعة معينة إذا اشتروا عضوية محددة.
كل شيء يعمل بشكل جيد. فقط أنه بعد بضعة أشهر أو أكثر (بشكل متقطع) تحدث هذه المشكلة وهي أن أحد المستخدمين العشوائيين لا يتم تعيينه إلى مجموعة معينة بعد شراء العضوية.
أريد التخلص من هذه المشكلة التي تحدث بشكل متقطع أيضًا.
إذا كان الخيار الثاني فقط هو الصحيح، فلن تعمل هذه التعليمات معك للأسف. يجب أن تكون جميع الخيارات الثلاثة صحيحة. يبدو أن لديك مزامنة تعمل. فقط حتى أفهم، لديك:
-
المكون الإضافي WP Discourse مثبت؛ و
-
رمز لمزامنة مجموعات WooCommerce الخاصة بك (على سبيل المثال، مثل هذا) ← هل أضفت هذا؟ هل يمكنك مشاركة ما أضفته؟
لا، أعتقد أن المكون الإضافي قام بكل الأتمتة، لا أتذكر وضع أي نوع من التعليمات البرمجية للتكامل
ربما يكون المكون الإضافي الخاص بي بالإصدار 2.2.3، وهو قديم لأن التحديث المتاح هو 2.4.1.
ربما يحدث هذا بسبب هذا الإصدار. لكنني فضولي بعض الشيء بشأن ما إذا كان التحديث سيضر بوظائفي الحالية أم لا.
لكي تتم مزامنة المجموعات بين WooCommerce و Discourse، تحتاج إلى بعض التعليمات البرمجية المخصصة. لن يقوم المكون الإضافي WP Discourse بذلك بنفسه. من المحتمل جدًا أن يكون لديك تطبيق للوظائف الموضحة هنا.
ستحتاج أولاً إلى تحديد كيفية حدوث المزامنة (لا يقتصر الأمر على المكون الإضافي WP Discourse) قبل أن أتمكن من مساعدتك بشكل أكبر.
لقد أضفت رمزًا إلى ووردبريس الخاص بي - الرمز الذي نشره @Ed_Bobkov:
//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’);
لقد أضفت وقمت بتشغيل run_full_wc_membership_sync عبر WP Crontrol أيضًا.
للأسف… لم تتم إضافة مستخدم ووردبريس لديه عضوية نشطة (معرف 112) إلى مجموعة discourse (معرف 41). ماذا فاتني هنا؟
نظرًا لأن هذا الأمر يربك الناس باستمرار، سأنشر دليلًا كاملاً حول كيفية مزامنة عضوية WooCommerce (سواء كان Wordpress أو Discourse هو موفر DiscourseConnect)
. يرجى التحقق هنا في بداية الأسبوع المقبل إذا لم أكن قد قمت بتنبيهك بشأنه بالفعل.
شكرا لك! حاولت فقط معرفة ذلك… لقد استخدمت حتى إضافة GitHub - paviliondev/discourse-woocommerce ولكن لم أنجح أيضًا