Created
March 10, 2023 13:24
-
-
Save raftaar1191/cba9795a41dc51309136b9cc02e92728 to your computer and use it in GitHub Desktop.
Redirect the user to checkout page when user try to visit the cart page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function gbol_template_redirect_cart_redirect( $permalink ) { | |
$cart_id = wc_get_page_id('cart'); | |
$checkout_id = wc_get_page_id('checkout'); | |
/** | |
* Skip if this is not a cart page | |
*/ | |
if ( $cart_id == $checkout_id ) { | |
return; | |
} | |
if ( ! is_cart() ) { | |
return; | |
} | |
if ( WC()->cart->get_cart_contents_count() > 0 ) { | |
// Redirect to checkout page | |
wp_redirect( wc_get_checkout_url(), '301' ); | |
exit; | |
} | |
} | |
add_action( 'template_redirect', 'gbol_template_redirect_cart_redirect' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment