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!
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