Woocommerce Membership synchronised accross multisite

admin2025-01-08  6

I have a WordPress Multisite installed (single domain + subfolders - each instance for a different country - so multiple languages but for now single currency - euro)

Users are automatically added to all instances (using "Join my site") - so you can use your login on all instances.

Now the problem is:

We want to launch a premium subscription-based membership (which would then active discounts).

When a user purchase a subscription on one site, he is granted a membership plan. Now the question is, how can I automatically activate it on all instances?

I was thinking to run some code when it's purchased that gift it to the same user on all other instances.

But maybe there is a plugin for that? Or something similar that I could use as base to build on?

Or some API doc that could be useful? I am bit clueless how to start here.

I have a WordPress Multisite installed (single domain + subfolders - each instance for a different country - so multiple languages but for now single currency - euro)

Users are automatically added to all instances (using "Join my site") - so you can use your login on all instances.

Now the problem is:

We want to launch a premium subscription-based membership (which would then active discounts).

When a user purchase a subscription on one site, he is granted a membership plan. Now the question is, how can I automatically activate it on all instances?

I was thinking to run some code when it's purchased that gift it to the same user on all other instances.

But maybe there is a plugin for that? Or something similar that I could use as base to build on?

Or some API doc that could be useful? I am bit clueless how to start here.

Share Improve this question asked Jul 20, 2017 at 9:17 Johann SavalleJohann Savalle 14 bronze badges 1
  • Did you solve your problem? I'm also trying to use memberships on two different wordpress installed on the same database, i've tried to use multisite wordpress also. Could you advise me on the better way to do it ? Thank you in advance for your answer :) have a nice day Ilhan. – Ilhan Mullier Commented Sep 20, 2019 at 8:13
Add a comment  | 

1 Answer 1

Reset to default 0

I have partially fixed the problem by writing a plugin where I generate a shortcode that I can use to progammatically after membership if user is logged in.

function wp_add_missing_membership ( $user_id, $plan_id ) {
    if ( ! function_exists( 'wc_memberships' ) ) {
       return;
    }
    $args = array(
    // Enter the ID (post ID) of the plan to grant at registration
    'plan_id'       => $plan_id,
    'user_id'       => $user_id,
);
wc_memberships_create_user_membership( $args );

// Get the new membership
$user_membership = wc_memberships_get_user_membership( $user_id, $args['plan_id'] );

// Add a note so we know how this was registered.
$user_membership->add_note( 'Membership imported from other site' );
}

function has_woocommerce_subscription($the_user_id, $the_product_id, $the_status) {
    $current_user = wp_get_current_user();
    if (empty($the_user_id)) {
            $the_user_id = $current_user->ID;
    }
    if (WC_Subscriptions_Manager::user_has_subscription( $the_user_id, $the_product_id, $the_status)) {
            return true;
    }
}

function bb_fixmembership ( $atts){
    $a = shortcode_atts( array('plan' => '0'), $atts );

    $what_s_the_plan = $a['plan'];
    if ( is_user_logged_in() ) {
            $user_id = get_current_user_id();
            if (wc_memberships_is_user_active_member($user_id, 'student')) {
               // do nothing - all is good
            } else {
                    wp_add_missing_membership($user_id, $what_s_the_plan);
            }
    }


}

add_shortcode( 'fixmembership', 'bb_fixmembership' );

Now I need to synchronise this with subscriptions accross websites - which I might do using hooks and user metadata.

I am just posting an answer since i don't see most post being answered :) so I thought this might help somebody else...

Will post when I fix it entirely.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267607a1209.html

最新回复(0)