Redirections for the cart page in WooCommerce

Redirect to the homepage if the cart page is empty or redirect to the checkout page if you try to access the cart page WooCommerce on your child site.

Snippet Type

Execute on Child Sites

Snippet

add_action('template_redirect', 'custom_cart_redirections');
function custom_cart_redirections() {
    if ( is_cart() ) {
        if ( WC()->cart->is_empty() ) {
            wp_redirect( home_url('/') );
            exit();
        } else {
            wp_redirect( wc_get_checkout_url() );
            exit();
        }
    }
}
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.