Stop the redirection after WooCommerce add to cart

China☆狼群 提交于 2019-12-06 08:46:16

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!