PhouvanhKCSV
7/4/2017 - 6:59 AM

Output a link to switch a subscription for the current user.

Output a link to switch a subscription for the current user.

<?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' ) );

    }

}