Skip to content

Instantly share code, notes, and snippets.

@thenbrent
Created November 13, 2013 19:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save thenbrent/7454868 to your computer and use it in GitHub Desktop.
Save thenbrent/7454868 to your computer and use it in GitHub Desktop.
Output a link to switch a subscription for the current user.
@mmasko
Copy link

mmasko commented Jun 18, 2017

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.

@metamn
Copy link

metamn commented Jul 3, 2017

// 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