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

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:

 

<?php // only copy this line if needed
/**
 * Check if product purchasing is retricted
 * Example: Add a restricted product notice
 * Display a top notice to non-members for members-only products
 */
function sv_wc_memberships_members_only_product_notice() {

        // bail if Memberships isn't active
        if ( ! function_exists( 'wc_memberships' ) ) {
                return;
        }

        $user_id = get_current_user_id();

        // Bail if the user is already a silver or gold member
        if ( wc_memberships_is_user_active_member( $user_id, 'silver' ) || wc_memberships_is_user_active_member( $user_id, 'gold' ) ) {
                return;
        }

        // Add our top notice if purchasing is restricted
        if ( wc_memberships_is_product_purchasing_restricted() ) {
                wc_print_notice( 'This is a preview! Only silver or gold members can purchase this product.', 'notice' );
        }
}
add_action( 'woocommerce_before_single_product', 'sv_wc_memberships_members_only_product_notice' );
 

 

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:

 

<?php // only copy this line if needed
/**
 * Check if a product has a member discount
 * Example: Display a member discount notice
 */
function sv_wc_memberships_member_discount_product_notice() {

        // bail if Memberships isn't active
        if ( ! function_exists( 'wc_memberships' ) ) {
                return;
        }

        // Set a discount end date
        $discount_ends = date_i18n( wc_date_format(), strtotime( '2015-12-31' ) );

        // Add our top notice if the member has a discount
        if ( wc_memberships_user_has_member_discount() ) {
                wc_print_notice( sprintf( 'Act fast! Your member discount ends on %s.', $discount_ends ), 'notice' );
        }
}
add_action( 'woocommerce_before_single_product', 'sv_wc_memberships_member_discount_product_notice' );

 

 

Conditional Checks: Content

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.

 

Conditional Checks: Members

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

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

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

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:

 

<?php // only copy if needed
/**
 * IF USING MEMBERSHIPS 1.7+
 * This snippet is likely not needed, you can set up a plan to grant access at registration
 *
 * However, this shows usage of programmatic membership creation for developers.
 */
/**
 * Programmatically create a user membership
 * Example: automatically grant membership access to a plan at WP user registration
 * Requires Memberships 1.3+
 *
 * @param int $user_id the ID of the newly created user
 */
function sv_wc_memberships_user_membership_at_registration( $user_id ) {

        // bail if Memberships isn't active
        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,
        );

        // magic!
        wc_memberships_create_user_membership( $args );

        // Optional: get the new membership and add a note so we know how this was registered.
        $user_membership = wc_memberships_get_user_membership( $user_id, $args['plan_id'] );
        $user_membership->add_note( 'Membership access granted automatically from registration.' );
}
add_action( 'user_register', 'sv_wc_memberships_user_membership_at_registration', 15 );

 

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

wc_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.

Table of Contents

All done here? Return to documentation overview →

Was this article helpful?
Dislike 0
Views: 28