Woocommerce Membership synchronised accross multisite

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.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)