WooCommerce - Skip cart page redirecting to checkout page

浪尽此生 提交于 2019-12-18 07:07:10

问题


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() );


来源:https://stackoverflow.com/questions/39655722/woocommerce-skip-cart-page-redirecting-to-checkout-page

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