Flash sale: Get up to 30% off themes and extensions. Ends April 26 at 3 pm UTC. Shop the sale.
  1. Documentation /
  2. WooCommerce Memberships Function Reference

WooCommerce Memberships Function Reference

For Developers: Checks & Functions

Please note that this document is a developer reference provided as a courtesy, as our support policy does not include customizations. This reference lists helpful functions and where to find them in the plugin. The plugin’s inline documentation has details on parameters / returned values, as this is meant to be a directory.
WooCommerce Memberships provides several methods for checking data on content restriction and user memberships. This reference includes helpful conditional checks and global functions for Memberships. Conditional checks return bool true or false. For example:
if ( wc_memberships_is_product_viewing_restricted() ) {
    echo 'MARCO!';
} else {
    echo 'POLO!';
}
This would check if viewing the current product is restricted to members or not so we can print a different message on the product page. We can also pass in a product id instead to check for a particular product.

Conditional Checks: Products

↑ Back to top
All product conditional checks are located in: /includes/wc-memberships-template-functions.php

wc_memberships_is_product_viewing_restricted()

@since
1.0.0
@param
int|null $post_id Optional, defaults to current post
@return
bool true if viewing is restricted
Checks if product viewing is restricted.

wc_memberships_is_product_purchasing_restricted()

@since
1.0.0
@param
int|null $post_id Optional, defaults to current post
@return
bool true if purchasing is restricted
Checks if product purchasing is restricted. Example:

wc_memberships_product_has_member_discount()

@since
1.0.0
@param
int|null $product_id Product ID: optional, defaults to current product
@return
bool true if product has discount
Checks if a member discount is present for the product.

wc_memberships_user_has_member_discount()

@since
1.0.0
@param
int|null $product_id Product ID: optional, defaults to current product
@return
bool true if current user has discount
Checks if the current user has a member discount on the product. Example:

Conditional Checks: Content

↑ Back to top

wc_memberships_is_post_content_restricted()

@since
1.0.0
@param
int|null $post_id Optional, defaults to current post
@return
bool true if content is restricted
Checks if a post or page is restricted.

wc_memberships_user_can( $user_id, $action, $target, $when = ” )

@since
1.4.0
@param
int $user_id user to check if has access
@param
string|array type of capabilities: ‘view’, ‘purchase’ (products only)
@param
array $target associative array of content type and content id to access to
@param
int|string UTC timestamp to compare for content access (optional, defaults to now)
@return
bool true if viewing is permitted
Checks if post, page, or product viewing is restricted by user. Can also check if product purchasing is restricted. Example:

Conditional Checks: Members

↑ Back to top
Looking to check for any active membership? This tutorial may be helpful.

wc_memberships_is_user_member()

@since
1.0.0
@param
int $user_id Optional, defaults to current user
@param
int|string $membership_plan Membership Plan slug, post object or related post ID
@return
bool true if user is a member of the plan
Checks if a user is a member for a particular plan.

wc_memberships_is_user_active_member()

@since
1.0.0
@param
int $user_id Optional, defaults to current user
@param
int|string $membership_plan Membership Plan slug, post object or related post ID
@return
bool true if user is an active member of the plan
Checks if a user is an active member for a particular plan. Example: display a notice to non-members / inactive members

Functions: Restricted Content

↑ Back to top

wc_memberships_restrict()

@since
1.0.0
@param
string $content
@param
string|int|array $membership_plans Optional: the membership plan or plans to check against; Accepts a plan slug, id, or an array of slugs or IDs. Default: all plans
@param
string $delay
@param
bool $exclude_trial
Restrict content to specific membership plans.See the [wcm_restrict] shortcode for an example.

wc_memberships_get_user_access_time

@since
1.4.0
@param
int $user_id User to get access time for
@param
array $target Associative array of content type and content id to access to
@param
string $action Type of access, ‘view’ or ‘purchase’ (products only)
@param
bool $gmt Whether to return a UTC timestamp (default false, uses site timezone)
@return
int|null Timestamp of start access time
Returns user access start timestamp (in site timezone) for content or a product. Note: for now $target only supports 'post' => id or 'product' => id

Functions: Membership Plans

↑ Back to top

wc_memberships_get_membership_plan()

@since
1.0.0
@param
int|string|\WC_Memberships_Membership_Plan $membership_plan Post object, ID or slug of the membership plan
@return
\WC_Memberships_Membership_Plan|false Returns the plan object or false on failure
Main function for returning a membership plan.

wc_memberships_get_membership_plans()

@since
1.0.0
@param
array $args Optional array of arguments, same as for get_posts()
@return
\WC_Memberships_Membership_Plan[]
Main function for returning all available membership plans.

Functions: User Memberships

↑ Back to top
Looking to check for any active membership? This tutorial may be helpful.

wc_memberships_get_user_active_memberships()

@since
1.7.0
@param
int $user_id WP user ID, defaults to current user
Returns an array of active memberships plan objects; empty if the user is not an active member. Supports statuses added for WooCommerce Subscriptions as well.
$memberships = wc_memberships_get_user_active_memberships( $user_id );

if ( ! empty( $memberships ) ) {
    // do something for this active member
}

wc_memberships_get_user_membership()

@since
1.0.0
@param
mixed $id Optional. Post object or post ID of the user membership, or user ID
@param
mixed $plan Optional. Membership Plan slug, post object or related post ID
@return
\WC_Memberships_User_Membership|false The User Membership or false if not found
Main function for returning a user membership; supports getting user membership by membership ID, Post object, or a combination of the user ID and membership plan id/slug/Post object. If no $id is provided, defaults to getting the membership for the current user. Example of getting a new, automatically created membership to then set a membership note:

wc_memberships_get_user_memberships()

@since
1.0.0
@param
int $user_id Optional, defaults to current user
@param
array $args Optional arguments
@return
\WC_Memberships_User_Membership[]|null array of user memberships
With version 1.4+, can also pass $args as a second parameter; currently accepts “status” and defaults to “any” status. However, this could be used to query all active memberships:
// get all active memberships for a user;
// returns an array of active user membership objects
// or null if no memberships are found
$user_id = get_current_user_id();
$args = array(
    'status' => array( 'active', 'complimentary', 'pending' ),
);
$active_memberships = wc_memberships_get_user_memberships( $user_id, $args );

wc_memberships_create_user_membership()

@since
1.3.0
@param
array $args Array of arguments {
  • @type int|string $user_membership_id membership / post ID for the membership (used when renewing)
  • @type int|string $plan_id membership plan / post object ID
  • @type int|string $user_id user ID for the membership
  • @type int|string $product_id the ID for the access-granting product
  • @type int|string $order_id the order in which access is granted
  • }
@param
string $action Action – either ‘create’ or ‘renew’ — when in doubt, use ‘create’
@return
\WC_Memberships_User_Membership|\WP_Error
Example:
<?php
// Automatically grant membership to a plan at registration
function sv_add_membership_at_registration( $user_id ) {
if ( ! function_exists( 'wc_memberships' ) ) {
return;
}
$args = array(
// Enter the ID (post ID) of the plan to grant at registration
'plan_id' => 253,
'user_id' => $user_id,
);
wc_memberships_create_user_membership( $args );
}
add_action( 'user_register', 'sv_add_membership_at_registration', 15 );

Functions: Other Functions

↑ Back to top

wc_memberships_get_memberships_from_subscription()

@since
1.6.0
@param
string|int|\WC_Subscription $subscription A Subscription id, object (or key for Subscriptions
@return
\WC_Memberships_User_Membership[] An array of User Membership objects or empty array if none found

wc_memberships_get_member_product_discount()

@since
1.4.0
@param
\WC_Memberships_User_Membership $user_membership The user membership object
@param
int|\WC_Product $product The product object or id to get discount for
@return
string discount value
Returns member discount (string) for a product.

wc_memberships_get_members_area_url()

@since
1.4.0
@param
int|\WC_Memberships_Membership_Plan $membership_plan Object or id
@param
string $member_area_section Optional, which section of the member area to point to
@param
int|string $paged Optional, for paged sections
@return
string Unescaped URL
Gets the member area URL for a particular membership plan.

wc_memberships_get_members_area_sections()

@since
1.4.0
@param
int|string $membership_plan Optional: membership plan id for filtering purposes
@return
array of sections
Gets the sections for the member area.

Table of Contents

↑ Back to top
All done here? Return to documentation overview →