Prafulla Kumar Sahu
Prafulla Kumar Sahu

Reputation: 9693

Adding a skip to checkout button woocmmerce

I want to add another button beside "add to cart" on single product page that will add the product to cart and also take the user to checkout page .

if I will use

add_filter ('add_to_cart_redirect', 'redirect_to_checkout');

function redirect_to_checkout() {
    global $woocommerce;
    $checkout_url = $woocommerce->cart->get_checkout_url();
    return $checkout_url;
}

this will override the add to cart button, but I want it to be like it is and want another button that will do this job ? is it possible with hooks ?

Upvotes: 0

Views: 53

Answers (1)

Prafulla Kumar Sahu
Prafulla Kumar Sahu

Reputation: 9693

I solved it by using a hidden field and two submit button, May be a dirty way but it solved my problem. It can be done in a better way using a checkbox in place of two button, *( if checkbox is checked go to checkout page ).

add_action( 'woocommerce_variable_skip_to_checkout', 'woocommerce_variable_skip_to_checkout', 30 );
if ( ! function_exists( 'woocommerce_variable_skip_to_checkout' ) ) {

    function woocommerce_variable_skip_to_checkout() {
        wc_get_template( 'single-product/add-to-cart/variation-skip-to-checkout-button.php' );
    }
}
add_filter ('woocommerce_add_to_cart_redirect', function() {
    if ( isset($_POST['skip_to_checkout']) && $_POST['skip_to_checkout'] == 'true' ){
        return WC()->cart->get_checkout_url();      
    }
} );

Template part

variation-skip-to-checkout-button.php

template holds the "skip to checkout" button and a hidden field, which you can replace with direct html in this function or a checkbox. and in place of woocommerce_variable_skip_to_checkout hook you should definitely prefer woocommerce_after_add_to_cart_button hook.

Upvotes: 1

Related Questions