Reputation: 57
Currently I am working on membership site where their are two types of membership
1) Free Membership 2) Premium membership
Based on these membership I want to change content of any pages For this I am using WooCommerce, WooCommerce Subscription and WooCommerce Membership.So Please tell me how I can get the membership plan after user logged to its account.
Thanks Mayank
Upvotes: 1
Views: 3551
Reputation: 5107
The wording of the question technically asks about Memberships, not Subscriptions.
If you're actually talking about the Woo Memberships plugin, then you will want this.
Memberships are usually created via a corresponding Subscription, but they can also be created manually, so be careful not to treat the two things as the same if a Subscription is not what you're intending to check for.
function myslug_current_user_has_membership( $membership_slug ) {
$has_membership = false;
if ( class_exists( 'WC_Memberships_User_Memberships' ) ) {
$memberships = new WC_Memberships_User_Memberships();
$user_memberships = $memberships->get_user_memberships( get_current_user_id() );
foreach ( $user_memberships as $membership ) {
$membership_plan = wc_memberships_get_membership_plan( $membership->plan_id );
if ( $membership_slug === $membership_plan->slug ) {
$has_membership = true;
break;
}
}
}
return $has_membership;
}
// example usage
if ( myslug_current_user_has_membership( 'premium-membership' ) ) {
echo 'this is a premium membership!';
}
Upvotes: 0
Reputation: 443
As of 2.0, use the function wcs_get_users_subscriptions(). It returns WC_Subscription[].
Upvotes: 0
Reputation: 176
Woocommerce subscriptions plugin has a class WC_Subscriptions_Manager that has a static function get_users_subscriptions. This function returns array containing user's subscriptions. So please use following code:
<?php
WC_Subscriptions_Manager::get_users_subscriptions(get_current_user_id());
?>
Upvotes: 5