SkyVerge WooCommerce Extensions

By default, WooCommerce Memberships includes a “Renew” link for an expired membership.

WooCommerce Memberships renew link

This allows the member to renew the membership by repurchasing the product (if available and at its current price) to resume the membership. This lets your member continue with any dripped content s/he already has access to once a set-length membership had expired if the member chooses to re-purchase.

However, we had a very interesting question recently: can you use this to provide an “upgrade” instead? For example, can you provide a 30-day “free” or “sample” membership, then use the renewal URL to prompt the member to purchase the one-year or “full” membership?

This is definitely possible with WooCommerce Memberships by changing the renewal URL so it doesn’t renew the current membership, but instead prompts the member to purchase a new one.

For the sake of this tutorial, I’ll refer to the initial membership (which is free in this case) as a “trial” membership, and the membership we want them to purchase at renewal as the “upgrade” membership.

1. Set up your membership products

You’ll need trial membership and upgraded membership products already created, and you should know the IDs of these products. You’ll also need the IDs of these membership plans (edit them and you’ll see the ID in the URL).

If you only want membership to be able to buy the upgraded membership product if they’ve used the trial membership, don’t forget to restrict it to the trial membership plan.

On a final note, if you only want members to be able to purchase a trial membership once, check out our tutorial on preventing repeat purchases of products.

2. Change the Memberships Renewal URL

The renewal URL is filterable via the wc_memberships_get_renew_membership_url filter located in:
woocommerce-memberships/templates/myaccount/my-memberships.php

This will let us adjust the renewal URL, and it passes in the user membership object so we can do it conditionally based on the membership (such as which plan we’re looking at).

/**
 * Changes the renewal URL for the trial membership
 *
 * @param string $url the renewal URL
 * @param \WC_Memberships_User_Membership $membership the user membership
 * @return string $url the updated renewal URL
 */
function sv_change_renewal_url( $url, $membership ) {
    
    // Use the ID of the trial plan we should change the "renew" link for
    if ( 311 === $membership->plan_id ) {
        // Enter the ID of the membership product for the upgraded membership purchase
        // This add to cart link will change for variable products
        $url = '/checkout/?add-to-cart=569';
    }
    return $url;
}
add_filter( 'wc_memberships_get_renew_membership_url', 'sv_change_renewal_url', 10, 2 );

If you’ve never used an add to cart link with WooCommerce, this tutorial from Nicola Mustone is very helpful for both simple and variable product links.

When a member clicks the renew link now, that renew link will take them to the checkout with the upgraded membership product in the cart.

This is the only function that’s mandatory to change the renewal link for the trial membership to a link to purchase the upgraded membership. If you’d like to change this button text or delete the old membership, you can continue with additional snippets to refine this workflow.

3. Change the Renewal Link Text

Since this action no longer truly “renews” the membership, you may want to change the text to something like “Renew for 1 Year” or “Upgrade to Gold” instead of “Renew”. This can also be done easily by filtering the “My Memberships” action buttons for that membership.

The wc_memberships_my_account_my_memberships_actions filter is available to do so, and it passes in the user membership as well so we can change the text only for our trial membership.

/**
 * Changes the renewal button text
 *
 * @param array $actions the actions for the membership under My Memberships
 * @param \WC_Memberships_User_Membership $membership the user membership
 * @return array $actions the updated actions
 */
function sv_change_membership_renewal_text( $actions, $membership ) {

    // Use the ID of the plan that we should change the "renew" link for
    if ( 311 === $membership->plan_id && $membership->is_expired() ) {
        $actions['renew']['name'] = 'Renew for 1 year';
    }

    return $actions;
}
add_filter( 'wc_memberships_my_account_my_memberships_actions', 'sv_change_membership_renewal_text', 10, 2 );

You can change the text to whatever you’d like, and this will now be displayed on the renewal button, and our new renewal URL will be used instead of the default “renew” URL.

WooCommerce Memberships New Renew Link

New renewal link

If you’d like to keep this expired membership in the member’s account, you can stop here. The last step is entirely optional, which is to delete this trial membership if the upgraded membership is purchased.

4. Delete the Trial Membership

You may not want to keep the trial membership around for the member, so this is entirely optional. The wc_memberships_grant_membership_access_from_purchase action is fired every time membership access is granted from a purchase, so we can hook into this to delete the trial membership when the upgraded membership is purchased by the member.

We’ll (1) check that the member is purchasing the upgraded membership plan, and (2) delete the trial membership if it exists and it’s expired.

/**
 * Changes the renewal URL for the trial membership
 *
 * @param string $url the renewal URL
 * @param \WC_Memberships_User_Membership $membership the user membership
 * @return string $url the updated renewal URL
 */
// Delete old membership post when new one purchased
function sv_remove_renewed_membership( $plan, $args ) {
    
    // Check that we're purchasing the "new" / renewal plan
    if ( 689 === $plan->id ) {
        $memberships = wc_memberships_get_user_memberships( $args['user_id'] );
        
        foreach ( $memberships as $membership ) {
            // Check to see if this member has the old plan and it's expired, 
            // then delete it if so
            if ( 311 === $membership->plan_id && $membership->is_expired() ) {
                 wp_delete_post( $membership->id );
            }
        }
    }
}
add_action( 'wc_memberships_grant_membership_access_from_purchase', 'sv_remove_renewed_membership', 10, 2 );

This will now delete the trial membership when the upgraded membership is purchased and “replace” it with the newly purchased membership.

WooCommerce Trial Membership Deleted

Trial Membership Upgraded


You can view the full code if desired — be sure to change the product and membership IDs as needed for your own use case.

Please note that this is a customization, so it’s not something we’ll modify as part of plugin support 🙂 The folks at Codeable could definitely help with this!

Published by Beka Rice

Beka leads product direction for SkyVerge and technical documentation. She spends a lot of time on research and interviews, but likes to write so she has an excuse to spend more time jamming out to anything from The Clash to Lady Gaga.

3 Comments

  1. […] you use WooCommerce Memberships, here’s a tutorial on renewing a trial membership with a paid plan from […]

  2. Thank you Beka

    This is exactly the solution I need for my site but how can i do this for more than one product. Also not absolutely sure where to add this code, I keep getting fatal errors on the “Cannot redeclare sv_change_renewal_url() (previously declared then” url I did get it to work on a single product but the next active product gave me the error.

    Thanks so much, James

  3. Wow, just stumbled on this post while trying to figure out how to reorganize some of our site’s memberships… we have multiple membership plans (think gold, silver, bronze) but they’re lifetime plans, and I’ve always been frustrated with how messy it looks when members upgrade to the next plan up and have multiple “levels” attached to their account.

    Could the “(2) delete the trial membership if it exists and it’s expired.” function be applied in our situation, where the original plan has NOT expired? I had to set up our member discounts in a really wacky way to account for people upgrading, and this would solve all of our problems!

Hmm, looks like this article is quite old! Its content may be outdated, so comments are now closed.