WooCommerce - Skip cart page redirecting to checko

2019-02-19 06:35发布

问题:

Our website is kohsamuitour.net. I have added custom code to skip the cart page on checkout, which works for all sales. This code:

function wc_empty_cart_redirect_url() {
return 'https://www.kohsamuitour.net/all-tours/';
}
add_filter( 'woocommerce_return_to_shop_redirect',     'wc_empty_cart_redirect_url' );

Now that does the job, but we also have a possibility to check booking availability. That can be found on the pages of private charters, i.e. this one: https://www.kohsamuitour.net/tours/kia-ora-catamaran/ .

Here the customer is being redirected to the cart, where I don't want that to happen as this is not a sale.

How can I make sure the 'Check booking availability' is also redirected to the checkout straight away?

回答1:

You can skip cart definitively, redirecting customers to checkout page when cart url is called.

To achieve this use this code snippet, that should do the trick:

// Function that skip cart redirecting to checkout
function skip_cart_page_redirection_to_checkout() {

    // If is cart page, redirect checkout.
    if( is_cart() )
        wp_redirect( WC()->cart->get_checkout_url() );
}
add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');

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

The code is tested and fully functional.


Edit: Since WooCommerce 3 replace wp_redirect( WC()->cart->get_checkout_url() ); by:

 wp_redirect( wc_get_checkout_url() );