skip to Main Content

I want to redirect to page with URL slug ‘join’ or post-id 471, what should I put behind wc_get_page_id?

add_action("template_redirect", 'redirection_function');
function redirection_function(){
    global $woocommerce;
    if( is_cart() && WC()->cart->cart_contents_count == 0){
       wp_safe_redirect( get_permalink( wc_get_page_id( 'join' ) ) ); 
     exit;
    }
}
add_action( 'template_redirect', 'cart_empty_redirect_to_join' );

Thanks!

2

Answers


  1. This should work. Let me know.

    add_action( 'template_redirect', 'redirect_if_empty_cart' );
    function redirect_if_empty_cart() {
    
        if ( !is_cart() ) return;
        global $woocommerce;
    
        if( is_cart() && $woocommerce->cart->cart_contents_count == 0) {
            wp_safe_redirect( get_permalink( 471 ) );
            exit;
        }
        
    }
    
    Login or Signup to reply.
  2. I had the same problem and I solved it in the following way.

    add_action( 'template_redirect', 'ani_empty_cart_redirect' );
    function ani_empty_cart_redirect(){
        $page_id = 602; // Here you can add the ID of the page you want
        if( is_cart() && WC()->cart->is_empty() ) {
            wp_safe_redirect(get_permalink($page_id));
            exit();
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search