Blog

WooCommerce Memberships: Add “View Subscription” Action to My Memberships

Here’s a quick question we’d gotten in a support thread: How can I add a link to “Cancel” a membership with it’s subscription to the “My Memberships” table?

Unfortunately, adding a link to cancel both a membership + it’s associated subscription isn’t really straight-forward — this is why the “cancel” action is removed in the first place when a memberships is tied to a subscription. This way, cancelling the billing record is the only way to cancel the membership (as cancelling a membership without stopping billing could cause headaches).

However, we can do something to work around this a bit: link to view the subscription associated with the user’s membership (if available) in the “My Memberships” table. The member area table actions are filterable, so you can remove or add your own actions pretty easily.

I’ve put together a simple snippet to do just that, and you can view the full snippet here.

We need it to do a few things:

  1. Get the subscriptions handler so we can check if the membership is tied to a subscription
  2. If tied to a subscription, add a new action to view the subscription in the account
  3. (Optional) rename the “View” action to “View Perks” or something similar, as we’ll now have 2 actions: “View Perks” (just “View” by default) and “View Billing”
function sv_wc_memberships_add_view_subscription_action( $actions, $user_membership ) {

    $integration = wc_memberships()->get_integrations_instance()->get_subscriptions_instance();

    if ( $integration->is_membership_linked_to_subscription( $user_membership ) ) {

        $subscription = $integration->get_subscription_from_membership( $user_membership->get_id() );
        
        $actions['view-subscription'] = array(
            'url'  => $subscription->get_view_order_url(),
            'name' => __( 'View Billing', 'my-textdomain' ),
        );
        
        $actions['view']['name'] = __( 'View Perks', 'my-textdomain' );
    }

    return $actions;
}
add_filter( 'wc_memberships_members_area_my-memberships_actions', 'sv_wc_memberships_add_view_subscription_action', 10, 2 );

You can adjust the text in this snippet for your action buttons as desired, but once this is implemented, the “My Memberships” table will now have a link to view the subscription / billing record, making it easy for the member to cancel the billing:

WooCommerce Memberships: add view subscription action

When this is clicked, it will immediately take the customer to the “View Subscription” page within the account:

WooCommerce Memberships: view subscription

This lets them update or cancel the billing record associated with the membership easily.

2 Comments

  • Edward Kay 4 months ago Reply

    Really useful tip here Beka.

    Should the filter now be wc_memberships_members_area_my-memberships_actions though? wc_memberships_my_account_my_memberships_actions is marked in the source as deprecated since 1.4.0

    • Beka Rice 4 months ago Reply

      Great catch Edward, thanks for that! Problems of having memorized a bunch of the hooks when writing the initial docs 😉 I’ll get this one updated here and in our snippets repo.

Submit a Comment