WooCommerce Memberships Snippet Samples

Here are some sample snippets helpful for tweaking the functionality of the WooCommerce Memberships plugin.

If you’re looking for more details on modifying Memberships or “For Developers” samples, please view our developer documentation.

Note that these snippets are UNSUPPORTED and are provided as a courtesy. We are unable to modify them or implement them on your site. If you’re not sure how to add custom code to your site or you want to modify these snippets, we recommend hiring a WooExpert to assist.

 

Member Area

 

Remove Cancel action from My Memberships table

The actions in the “My Memberships” table can be removed if you don’t want to allow your members to manage their own memberships. This snippet removes the “Cancel” action from the “My Memberships” table in the account area.

Note: Memberships automatically does this for you if a membership is tied to a subscription, forcing cancellation via the billing instead.

  <?php
  /**
  * Only copy the opening php tag if needed
  */
  function sv_edit_my_memberships_actions( $actions ) {
  // remove the “Cancel” action for members
  unset( $actions[‘cancel’] );
  return $actions;
  }
  add_filter( ‘wc_memberships_members_area_my-memberships_actions’, ‘sv_edit_my_memberships_actions’ );
  add_filter( ‘wc_memberships_members_area_my-membership-details_actions’, ‘sv_edit_my_memberships_actions’ );

 

 

 

Remove End Date Column from My Memberships

Removes the “End Date” column from being displayed for all user memberships.

  <?php
  // Only copy opening php tag if needed
  // Remove the “End Date” column from the My Memberships table
  function sv_remove_membership_end_date_column( $columns ) {
  unset( $columns[‘membership-end-date’] );
  return $columns;
  }
  add_filter( ‘wc_memberships_my_memberships_column_names’, ‘sv_remove_membership_end_date_column’ );

 

 

 

Remove Type Column from My Content Area

The “My Content” section of the Member Area will show the content type (Post, Page, Project, Forum, etc) as this paginated section shows all accessible content (posts, pages, custom post types) on the site. This snippet removes the “Type” column if its display isn’t needed.

  <?php
  // Only copy opening php tag if needed
  // Removes the “Type” column from the “My Content” section of the member area
  function sv_members_area_content_remove_column( $columns ) {
   
  // unset the “type” column, which shows post, page, project, etc
  if ( isset( $columns[‘membership-content-type’] ) ) {
  unset( $columns[‘membership-content-type’] );
  }
   
  return $columns;
  }
  add_filter( ‘wc_memberships_members_area_my_membership_content_column_names’, ‘sv_members_area_content_remove_column’ );

 

 

 

Remove Description Column from My Products Area

The paginated “My Products” section of the Member Area will show all accessible products for the member (those the plan grants viewing / purchasing for). This snippet removes the “Description” / Excerpt column if its display isn’t needed.

  <?php
  // Only copy opening php if needed
  // Removes the product short description / excerpt column from “My Products”
  function sv_members_area_products_table_columns( $columns ) {
  if ( isset( $columns[‘membership-product-excerpt’] ) ) {
  unset( $columns[‘membership-product-excerpt’] );
  }
  return $columns;
  }
  add_filter(‘wc_memberships_members_area_my_membership_products_column_names’, ‘sv_members_area_products_table_columns’, 10, 1 );

 

 

 

Blog & Posts

Show Lock Icon on Restricted Posts

This snippet requires your site to have loaded FontAwesome already, as it uses the FontAwesome lock icon. It will show a lock next to the title of any post the member does not have access to yet, or any restricted posts for non-members.

 

<?php // only copy if needed
/**
 * Display a FontAwesome lock icon next to the post title if a member does not have access
 *  with WooCommerce Memberships.
 *
 * REQUIRES FontAwesome to be loaded already
 *
 * @param string $post_title the post title
 * @param int $post_id the WordPress post ID
 * @return string the updated title
 */
function sv_wc_memberships_add_post_lock_icon( $title, $post_id ) {

        if ( is_admin() ) {
                return $title;
        }

        // show the lock icon if the post is restricted, or access is delayed
        if (   ! current_user_can( 'wc_memberships_view_delayed_post_content',    $post_id )
            || ! current_user_can( 'wc_memberships_view_restricted_post_content', $post_id ) ) {

                $title = "<i class='fa fa-lock' aria-hidden='true'></i> {$title}";
        }

        return $title;
}
add_filter( 'the_title', 'sv_wc_memberships_add_post_lock_icon', 10, 2 );
 

 

 

Shop Pages

 

Remove Member Discount Badges

The member discount badge’s HTML is filterable, and therefore it can be removed completely using this snippet:

add_filter( 'wc_memberships_member_discount_badge', '__return_empty_string' );

 

Disable Discount Stacking

By default, Memberships will assume that members should always have all benefits of a membership plan. However, this could potentially result in a member receiving 2 discounts or more on a product if the member has multiple plans with discounts.

If you want to disable this behavior, Memberships can instead choose the best discount only instead of stacking discounts — add this snippet to do so:

add_filter( 'wc_memberships_allow_cumulative_member_discounts', '__return_false' );

Integrations

 

Add Social Login Buttons to Restriction Messages

You can add WooCommerce Social Login buttons into the “content restricted” notices to make it easier for members with a linked account to log in.

We do have this on our Memberships / Social Login roadmaps to add support for. Until this happens within the plugins themselves, you could add this code snippet to show login buttons in content restriction notices.

Other Snippets

 

Grant Access only from Completed Orders

By default, Memberships grants access to customers for any processing or completed order on your site since these represent paid orders. If you’d like access to only be granted when an order is ‘completed’, but not while it’s ‘processing’, you can use this snippet:

 

<?php // only copy this line if needed
/**
 * Removes membership access for processing orders
 * so access is only granted at when orders are complete
 */
function sv_wc_memberships_remove_processing_access() {

        // make sure Memberships is active first
        if ( function_exists( 'wc_memberships' ) ) {

                remove_action( 'woocommerce_order_status_processing', array( wc_memberships()->get_plans_instance(), 'grant_access_to_membership_from_order' ), 9 );
        }

        // Add support for Teams for Memberships but make sure it is active first
        if ( function_exists( 'wc_memberships_for_teams' ) ) {

                remove_action( 'woocommerce_order_status_processing', array( wc_memberships_for_teams()->get_orders_instance(), 'process_team_actions_for_order' ), 10 );
        }
}
add_action( 'init', 'sv_wc_memberships_remove_processing_access' );

 

If you want to add statuses (such as custom statuses) that grant access, our developer documentation can help.

 

Create My Memberships Shortcode

If you want to output the “My Memberships” table outside of the “My Account” area, you can create a shortcode to do so. Please have a look at this snippet.

More Snippets

We keep a collection of team snippets for our plugins in this repository. However, the same policy applies — we cannot support these snippets, assist with implementation, or assist with further customizations.

Table of Contents

All done here? Return to documentation overview →


Note that these snippets are UNSUPPORTED and are provided as a courtesy. We are unable to modify them or implement them on your site. If you’re not sure how to add custom code to your site or you want to modify these snippets, we recommend hiring an Woo Expert to assist.
Was this article helpful?
Dislike 0
Views: 56