WooCommerce “Checkout Now” button

Okay so I want to add an additional button to the single product page that says “Checkout Now”. This checkout now button should go directly to the checkout page, not the cart page (like the regular “add to cart” button).

I can’t for the life of me figure out how to get this done.

I’ve found a snippet that achieves this functionality, but it changes the global URL (i.e. changes the “Add to cart” url as well).

add_filter('add_to_cart_redirect', 'themeprefix_add_to_cart_redirect');
function themeprefix_add_to_cart_redirect() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}

Anyone have any idea how to get this working?

Solutions Collecting From Web of "WooCommerce “Checkout Now” button"

Try this –

Add your button inside a form and write down script to add product to the cart and redirect user directly to the checkout page. Suppose, following is the button that you can put on single product page using a WooCommerce hook.

<button name="checkout_now"> Checkout Now </button>

PHP code:

    global $woocommerce;

    $post_id = get_the_ID(); //you can directly use get_the_ID() as you are on single product page or you can also make use of global $product object and then $product->ID

    if( isset($_POST['checkout_now']) ){      //assuming the form method is 'post'
        $woocommerce->cart->add_to_cart( $post_id );
        $checkout_url = $woocommerce->cart->get_checkout_url();
        wp_redirect($checkout_url);
        exit;
    }