-1

I'm currently working on a Woocommerce website mainly on Elementor Pro with WooCommerce Subscription.

My client is asking for a subscription "Upgrade/Downgrade" button for a custom page that we've built and since I'm honestly not that advanced in PHP, I'm currently stuck on how to create that button.

The subscription is under a variable product that has Monthly and Yearly options. As far as I know, upgrading a product is different from buying the subscription again with a higher value since the URL for switching has the the additional "?switch-subscription=XXXX&item=XX&_wcsnonce=XXXXXXXXXX" included in the address that is uniquely different per user.

I just want to know if it's possible to generate the proper URL for subscription switching so that I can add that link to a button, or a better workaround.

Thanks.

EpikMint
  • 29
  • 3

1 Answers1

2

Call the function below for printing the switch link for subscription.

WC_Subscriptions_Switcher::print_switch_link( $item_id, $item, $subscription );

The definition is below.

/**
 * Adds an Upgrade/Downgrade link on the View Subscription page for each item that can be switched.
 *
 * @param int $item_id The order item ID of a subscription line item
 * @param array $item An order line item
 * @param object $subscription A WC_Subscription object
 * @since 1.4
 */
public static function print_switch_link( $item_id, $item, $subscription ) {

    if ( wcs_is_order( $subscription ) || 'shop_subscription' !== $subscription->get_type() || ! self::can_item_be_switched_by_user( $item, $subscription ) ) {
        return;
    }

    $switch_url  = esc_url( self::get_switch_url( $item_id, $item, $subscription ) );
    $switch_text = get_option( WC_Subscriptions_Admin::$option_prefix . '_switch_button_text', __( 'Upgrade or Downgrade', 'woocommerce-subscriptions' ) );
    $switch_link = sprintf( '<a href="%s" class="wcs-switch-link button">%s</a>', $switch_url, $switch_text );

    echo wp_kses( apply_filters( 'woocommerce_subscriptions_switch_link', $switch_link, $item_id, $item, $subscription ), array( 'a' => array( 'href' => array(), 'title' => array(), 'class' => array() ) ) ); // phpcs:ignore WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
}
mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • Hi. Thanks for this. I just want to ask on how/where to add this at least as a HTML or Elementor button? I'm admittedly not good in PHP and not sure where to insert this. – EpikMint Feb 26 '22 at 15:07
  • Is this possible to use the function as a shortcode? Thanks. – EpikMint Feb 26 '22 at 17:59