Last active
March 11, 2022 01:36
-
-
Save bekarice/cac24f82309fe4e5149a to your computer and use it in GitHub Desktop.
WooCommerce Memberships: Change a Renewal Link to Purchase a Different Membership
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Changes the renewal link for a particular membership to purchase a new one | |
// Old membership will be deleted upon purchase if 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 | |
*/ | |
function sv_change_renewal_url( $url, $membership ) { | |
// Use the ID of the plan that we should change the "renew" link for | |
if ( 311 === $membership->plan_id ) { | |
// Enter the add to cart URL of the upgrade membership product that should be purchased | |
$url = '/checkout/?add-to-cart=99'; | |
} | |
return $url; | |
} | |
add_filter( 'wc_memberships_get_renew_membership_url', 'sv_change_renewal_url', 10, 2 ); | |
/** | |
* 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 trial membership plan to change the button text | |
if ( 311 === $membership->plan_id && $membership->is_expired() ) { | |
$actions['renew']['name'] = 'Renew for 1 year'; | |
} | |
return $actions; | |
} | |
add_filter( 'wc_memberships_members_area_my-memberships_actions', 'sv_change_membership_renewal_text', 10, 2 ); | |
/** | |
* Deletes the expired trial membership when the upgraded membership is purchased | |
* | |
* @param \WC_Memberships_Membership_Plan $plan the membership plan | |
* @param array $args user data from the new membership | |
*/ | |
function sv_remove_renewed_membership( $plan, $args ) { | |
// Check that we're purchasing the upgraded plan before doing anything | |
if ( 689 === $plan->id ) { | |
// Get all of this user's memberships | |
$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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is absolutely great solution, unfortunately I am getting error on the third function which should remove trial membership. First two are working great.
Error is: Cannot redeclare sv_remove_renewed_membership()