For Developers: Checks & Functions
↑ Back to topPlease 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 topAll 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.
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.
Conditional Checks: Content
↑ Back to topwc_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.
Conditional Checks: Members
↑ Back to topLooking 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 topwc_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 topwc_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 topLooking 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.
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:
wc-memberships-create-member.php content:
<?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 topwc_memberships_get_memberships_from_subscription()
@since
1.6.0
@param
string|int|\WC_Subscription $subscription
A Subscription ID, object (or key for Subscriptions < 2.0.0)
@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.
User Documentation
↑ Back to topAll done here? Return to documentation overview →