Forked from thenbrent/switch-subscription-link.php
Last active
October 23, 2022 06:17
-
-
Save jrglass/5dd19867e2d54e69e02d7bebf0c10810 to your computer and use it in GitHub Desktop.
Woocommerce Subscriptions: Output a link to switch a subscription for the current user.
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 | |
// Make sure WooCommerce Subscriptions is active | |
if ( class_exists( 'WC_Subscriptions_Switcher' ) ) { | |
// Get the key for the customer's current subscription | |
$subscription_key = WC_Subscriptions_Manager::get_subscription_key( $order_id, $product_id ); | |
// Check if the current subscription can be switched by the customer | |
if ( WC_Subscriptions_Manager::can_subscription_be_changed_to( 'new-subscription', $subscription_key, get_current_user_id() ) ) { | |
// Print the link to initiate the switching process | |
printf( '<a href="%s">%s</a>', WC_Subscriptions_Switcher::get_switch_link( $subscription_key ), __( 'Choose a new subscription', 'textdomain' ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment