A
A
Alex_872021-08-30 13:04:57
WooCommerce
Alex_87, 2021-08-30 13:04:57

How to redirect the visitor to the main page when removing the last item from the cart?

Hello!
I used the woocommerce_cart_item_removed hook to solve this issue.

What happens when you work with this hook?

When removing the last item from the cart, it redirects to the page.php page, where the following information is displayed.

612cacca8f632007433527.png

Only after clicking on the link - Return to the store, the system

redirects

to the store page. page

Code written in function.php

add_action( 'woocommerce_cart_item_removed', 'empty_cart_redirect' );
function empty_cart_redirect(){
  if( is_cart() && WC()->cart->is_empty() ) {

    wp_safe_redirect( esc_url( home_url( '/' ) ) );
    // echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) );
    // wp_redirect( home_url( '/' ) );

    exit();
  }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question