skip to Main Content

I’m working on an e-commerce site based on WooCommerce and the Divi theme. Specifically, when the cart is empty, I get a blank page (though the header and footer are present). I’d like to know if there is a way to use a custom page for the empty cart.

I’ve already tried adding this code to functions.php:

function custom_redirect_empty_cart() {
    // Replace 123 with the ID of your custom page
    $empty_cart_page_id = 123; // Change this to your page ID

    if ( WC()->cart->is_empty() && !is_cart() && !is_checkout() ) {
        wp_redirect( get_permalink( $empty_cart_page_id ) );
        exit;
    }
}
add_action( 'template_redirect', 'custom_redirect_empty_cart' );

But it didn’t work.

Can you help me? Thank you very much!

2

Answers


  1. Could you please try to cross check the page id is correct in the code, also please try to reset the permalink.

    You can do this by going to Settings > Permalinks and then click on "Save Changes".

    Could you please try to copy the cart-empty.php file from the WooCommerce plugin (wp-content/plugins/woocommerce/templates/cart/cart-empty.php) to your theme folder, maintaining the path like so: wp-content/themes/divi/woocommerce/cart/cart-empty.php.

    Edit this new file to show the content you want to display to user when the cart is empty.

    You can find more information regarding empty cart template on this.

    https://woocommerce.github.io/code-reference/files/woocommerce-templates-cart-cart-empty.html

    Login or Signup to reply.
  2. The page you are redirecting to must be created + the ID must be the ID, not the page address. The working code is below.

    add_action( 'template_redirect', 'custom_redirect_empty_cart' );
    
    function custom_redirect_empty_cart(){
        $empty_cart_page_id = 52;
    
        if( WC()->cart->is_empty() && is_cart() && !is_checkout() ){
            wp_safe_redirect(get_permalink($empty_cart_page_id));
            exit;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search