Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop the redirection after WooCommerce add to cart

I wish to completely remove any redirection after a user click on the ADD TO CART button.

Actually I am not using the products page.
I am using a simple button with the link to the product, like this: ?add-to-cart=492.

My user will click on several "add to cart" buttons on my page, so he cant be redirected to any page after clicking in the first button.

At the end of the page he will find a CHECKOUT button to pay and that is it.

Any ideas how to achieve this?

Thanks

like image 376
rfmarchetti Avatar asked Jan 22 '26 02:01

rfmarchetti


2 Answers

Update:

Your simple html "add-to-cart" button links are actually for example like that (the href value):

<a href="http://my-domain.com/site2/?add-to-cart=492" target="_self" class="button white is-larger carrinho">
    <span>ESCOLHER PACOTE</span>
</a>

So they are redirected each time to your home page


2 SOLUTIONS:

1) Use the WooCommerce short code [add-to-cart] this way:**

  • Without price: [add_to_cart id="492" show_price="false"]
  • With the price: [add_to_cart id="492"]

2) Html code in your page text editor - To prevent redirections, the href attribute should be:

<a href="?add-to-cart=492" class="button white is-larger carrinho">
    <span>ESCOLHER PACOTE</span>
</a>

This time your customers will not be redirected as before


THE CHECKOUT BUTTON

To finish, here is a custom shortcode that will output the "Proceed to checkout" button:

if( !function_exists('proceed_to_checkout_button') ) {
    function proceed_to_checkout_button() {

        $checkout_url = wc_get_checkout_url();
        $button_txt = __('Proceed to checkout', 'woocommerce');

        $output = '<div class="wc-proceed-to-checkout">
            <a href="'. $checkout_url .'" class="checkout-button button alt wc-forward">
                '. $button_txt .'
            </a>
        </div>';

        return $output;
    }
    add_shortcode( 'checkout_button', 'proceed_to_checkout_button' );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Usage: just add this to your pages editor: [checkout_button]


Original answer:

First, In WooCommerce settings you need to:

  • Enable **Ajax on add-to-cart button (Woocommerce > Settings > Products > Display)
  • Disable the add-to-cart button redirection (Woocommerce > Settings > Products > Display)

Then you can add a custom "Proceed to checkout" button using:

  • Any classic WordPress menu (Appearance > Menus)
  • With that custom code on single product pages and product archives:
add_action('woocommerce_after_single_product', 'custom_checkout_button', 100);
add_action('woocommerce_after_shop_loop', 'custom_checkout_button', 100);
function custom_checkout_button() {

    $checkout_url = wc_get_checkout_url();
    $button_txt = __('Proceed to checkout', 'woocommerce');

    ?>
        <div class="wc-proceed-to-checkout">
            <a href="<?php echo $checkout_url; ?>" class="checkout-button button alt wc-forward">
                <?php echo $button_txt ?>
            </a>
        </div>
    <?php
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

The button "Proceed to checkout" will be show at the bottom of this pages.


If you want to skip the cart page:

add_action('template_redirect', 'skip_cart_page_redirecting_to_checkout');
function skip_cart_page_redirecting_to_checkout() {

    // If is cart page and cart is not empty
    if( is_cart() && ! WC()->cart->is_empty() )
        wp_redirect( wc_get_checkout_url() );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

All code is tested and works.

like image 65
LoicTheAztec Avatar answered Jan 23 '26 21:01

LoicTheAztec


Like in the woocommerce itself does in the product-code shortcode you could just use the filter for the simple-add to cart button 'woocommerce_add_to_cart_form_action'.

// Change form action to avoid redirect.
add_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );
like image 45
Michael Schober Avatar answered Jan 23 '26 21:01

Michael Schober