skip to Main Content

I wanted to redirect the customer on cart page to the shop page – when emptying the cart there.
I tried this code:

add_action( 'template_redirect', 'empty_cart_redirection' );
function empty_cart_redirection(){
    if( is_cart() ) :
    
        // Here set the Url redirection
        $url_redirection = get_permalink( wc_get_page_id( 'shop' ) );
    
        // 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;
}

This only works when reloading the page or when someone tries to go to the cart (when its empty). Does anyone know how to make it work when emptying the cart in the cart page?

Thank you so much.

2

Answers


  1. Chosen as BEST ANSWER

    first of all: thank you for your answer. I'm using a Astra Child Theme. I tried to create a new file footer.php and put the code you gave me there and it's working fine (this action). The problem is that then a lot of things get strange on the website. Secondly, I copied the Astra Footer file and past the code in this file; uploaded it into the child theme and then everything is okey but the redirection is not working anymore.

    I don't know a lot about code, so i don't know if i'm doing something wrong.

    Anyways, Thank You!!!!!


  2. You have to add code to the footer. You can use the wp_footer action hook to add javascript code on the footer. You can use the updated_wc_div trigger as well and you can check the .cart_item length. try the below code.

    Using wc_cart_emptied

    function redirect_to_shop_when_removing_item_and_cart_is_empty(){
        if( is_cart() ){
            // Here set the Url redirection
            $url_redirection = get_permalink( wc_get_page_id( 'shop' ) );
            ?>
            <script type="text/javascript">
                (function($){
    
                    $(document.body).on( 'wc_cart_emptied', function(){
                        if ( $( '.woocommerce-cart-form' ).length === 0 ) {
                            $(window.location).attr('href', '" . $url_redirection . "');
                            return;
                        }
                    });
    
                })(jQuery);
            </script>
            <?php
        }
    }
    add_action( 'wp_footer', 'redirect_to_shop_when_removing_item_and_cart_is_empty', 10, 1 );
    

    Using updated_wc_div

    function redirect_to_shop_when_removing_item_and_cart_is_empty(){
        if( is_cart() ){
            // Here set the Url redirection
            $url_redirection = get_permalink( wc_get_page_id( 'shop' ) );
            ?>
            <script type="text/javascript">
                (function($){
    
                    $(document.body).on('updated_wc_div', function () {
                        if( $('.cart_item').length < 1 ){
                            window.location.href = "<?php echo $url_redirection; ?>";
                        }
                    });
                })(jQuery);
            </script>
            <?php
        }
    }
    add_action( 'wp_footer', 'redirect_to_shop_when_removing_item_and_cart_is_empty', 10, 1 );
    

    Both triggers is tested and working fine.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search