WooCommerce: mixing Cart and Checkout page

When using WooCommerce for WordPress, some clients like to have the Cart and the Checkout area in the same page.

This is possible to do putting the two shortcodes together in a certain page, and then tweaking the WC settings in Settings > Checkout > Checkout Pages to make both Cart and Checkout to share the same page.

However, this leads to some redirection errors when you empty the Cart. It usually goes to a “Cart Empty” message, but you can’t have an empty cart and a Checkout process at the same time.

Here’s a snippet that will allow us to redirect to /shop instead, so people can directly continue shopping when they eventually empty their cart:

Place this in functions.php:

add_action( 'init', 'woocommerce_empty_cart_redirect' );
function woocommerce_empty_cart_redirect() {
global $woocommerce;
if ( $_SERVER["REQUEST_URI"] == "/checkout/" && !sizeof( $woocommerce->cart->get_cart() )) {
wp_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit;
}
}

Tell me if it works!
Cheers,

0 Give it a like!

Leave a Reply

Your email address will not be published. Required fields are marked *