• Resolved albridi

    (@albridi)


    Hi there ?? !

    I need to redirect the user to a custom “empty cart” page. I’ve been trying to use this code:

    add_action( 'template_redirect', 'empty_cart_redirect' );
    function empty_cart_redirect(){
        if( is_cart() && WC()->cart->is_empty() ) {
            wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
            exit();
        }
    }

    but changing the line:

    wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );

    to my custom page:

    wp_safe_redirect( get_permalink( wc_get_page_id( 'my-empty-cart' ) ) );

    Unfortunately this is not working.

    Is there a way to redirect to a custom “empty cart” page when the user removes all items from the cart?

    Kind regards.

    • This topic was modified 2 years, 5 months ago by albridi.
    • This topic was modified 2 years, 5 months ago by albridi.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Saif

    (@babylon1999)

    Hello @albridi,

    You can achieve it with something like this:

    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( get_page_by_title('Home')));
        }
    }

    Inspiration came from this StackOverflow comment.

    I’ve tested it on my site and it’s redirecting to my HomePage when accessing the cart page with no products. ??

    You can change the ‘Home’ to the title of that custom page.

    Hope it helps!

    Thread Starter albridi

    (@albridi)

    Hi @babylon1999, thanks for supporting ?? !

    The code works great, but not if you remove an item while you’re on the cart page. I tried the following code by changing the line “get_permalink( wc_get_page_id( ‘shop’ ) );” with the one you provided: “get_permalink( get_page_by_title(‘Home’));”:

    add_action( 'template_redirect', 'empty_cart_redirection' );
    function empty_cart_redirection(){
        if( is_cart() ) :
        
        // Here set the Url redirection
        $url_redirection = get_permalink( get_page_by_title('Ups 404'));
        
        // When trying to access cart page if cart is already empty  
        if( WC()->cart->is_empty() ){
            wp_safe_redirect( $url_redirection );
            exit();
        }
        
        // When emptying cart on cart page
        wc_enqueue_js( "jQuery(function($){
            $(document.body).on( 'wc_cart_emptied', function(){
                if ( $( '.woocommerce-cart-form' ).length === 0 ) {
                    $(window.location).attr('href', '" . $url_redirection . "');
                    return;
                }
            });
        });" );
        endif;
    }

    It worked ?? . Unfortunately, it performs the redirect after removing items from the “cart” and this creates a time lapse between when the “cart” is emptied and when the “empty cart” page is loaded, which is filled with an empty version of the “cart” page. This creates a very bad display sensation.

    Is there a way to make the transition between the “cart” and “empty cart” pages faster and more efficient?

    Kind regards.

    Hi @albridi

    I understand that you want to redirect to a custom page once the cart is emptied on a faster loading time.

    These forums are meant for general support with the core functionality of WooCommerce itself. What you want to achieve would require customization to do it. Since custom coding is outside our scope of support, I am leaving this thread open for a bit to see if anyone can chime in to help you out.

    For questions related to development and custom coding, your best bet is to ask on any of these channels for support. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, too.

    WooCommerce Developer Resources Portal
    WooCommerce Advanced Facebook group
    WooCommerce Community Forum
    WooCommerce Developer Slack Channel.
    – Hire a WooCommerce Expert

    Thread Starter albridi

    (@albridi)

    Hi @xue28, thanks for supporting ?? !

    I will try the support channels you suggest.

    Kind regards.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Redirect to a specific page when cart is emptied’ is closed to new replies.