1

I know it’s possible to send customer directly to the checkout page by appending “add-to-car=product_id” to the checkout endpoint like this:

yourwebsite.com/checkout/?add-to-cart=12345

I was wondering if it’s a way to do the same when a product has subscription options? I use Woocommerce Subscriptions plugin to manage subscriptions and it doesn't create a separate variant for subscription product.

system0102
  • 129
  • 2
  • 5

1 Answers1

1

Based on these answers:

You can create a PHP script to change the redirect url after adding a product to the cart (by checking the product class type).

Furthermore, using the woocommerce_add_to_cart_redirect hook you have two parameters: $url and $adding_to_cart (product object). Here you find the documentation.

Then:

// redirects to checkout if the product added to the cart is a subscription
add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_to_checkout_if_product_is_subscription' );
function redirect_to_checkout_if_product_is_subscription( $url, $product ) {
    if ( class_exists( 'WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
        global $woocommerce;
        $checkout_url = $woocommerce->cart->get_checkout_url();
        return $checkout_url;
    }
    return $url;
}

The code must be added to your active theme's functions.php file.

Vincenzo Di Gaetano
  • 3,892
  • 3
  • 13
  • 32