0

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?

closed as off-topic by Mayeenul Islam, Pieter Goosen, Howdy_McGee, Mark Kaplun, Brad Dalton Jul 31 '15 at 9:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1

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;
    }
  • Thanks! Where would I add the PHP script? – user2766888 Jul 30 '15 at 8:58
  • in your functions.php – melvin Jan 19 '18 at 11:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.