Skip to content

Instantly share code, notes, and snippets.

@raftaar1191
Created March 10, 2023 13:24
Show Gist options
  • Save raftaar1191/cba9795a41dc51309136b9cc02e92728 to your computer and use it in GitHub Desktop.
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
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