Created
November 13, 2013 19:27
-
-
Save thenbrent/7454868 to your computer and use it in GitHub Desktop.
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' ) ); | |
} | |
} |
// Get plan switch url
// - https://gist.github.com/thenbrent/7454868
function ws_get_plan_switch_url($plan_id) {
$url = '';
$user_subscriptions = wcs_get_users_subscriptions();
if (ws_isset($user_subscriptions)) {
foreach ($user_subscriptions as $subscription) {
$subscription_order_id = $subscription->order->id;
$subscription_key = WC_Subscriptions_Manager::get_subscription_key( $subscription_order_id, $plan_id);
if ($subscription_key) {
// get plan parent
$plan_parent_id = wp_get_post_parent_id($plan_id);
$url = WC_Subscriptions_Switcher::get_switch_url(
$subscription_order_id,
array('product_id' => $plan_parent_id),
$subscription);
}
}
}
return $url;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would it be possible to provide a little detail on how to integrate this? What does 'new-subscription' pertain to? Is it a variable? Same question for __( 'Choose a new subscription', 'textdomain' ).
I've been trying to figure out how to make custom links so customers can switch between subscriptions.