I had a customer who needed to show Woocommerce cart and checkout page on the same page. So, if you need a similar solution then in this post I’m going to show you how to merge Woocommerce cart and checkout page.
Before you take a look at the code snippets I’m using it would be wise to see the video here below. This way it’ll be a bit easier to understand what you should do.
Video: How to merge Woocommerce cart and checkout page?
And this is what you’ll going to accomplish.
Step 1: Merge Woocommerce cart and checkout page
In this step we’re going to add a Woocommerce cart table above the checkout table Thus, grab this code here below and add it inside your child theme’s functions.php file or better yet, use Code Snippets plugin for it. There is nothing to configure, just paste it as it is.
// 1. Add Woocommerce cart page on the checkout page
add_action( 'woocommerce_before_checkout_form', 'add_cart_on_checkout', 5 );
function add_cart_on_checkout() {
if ( is_wc_endpoint_url( 'order-received' ) ) return;
echo do_shortcode('[woocommerce_cart]'); // Woocommerce cart page shortcode
}
Step 2: Redirect Woocommerce cart page to checkout page
This part is optional and use it if you don’t want the “View cart” buttons to direct your users to cart page. Also, use it if you don’t want your users to access Woocommerce cart page directly.
So, grab this code and add it to you functions.php file or Code Snippets code box. Pay attention that “cart” and “/checkout/” parts are your cart and checkout pages slugs. If you have any other slugs (karte or kassa for example) then change them accordingly.
// 2. Redirect cart page to checkout
add_action( 'template_redirect', function() {
// Replace "cart" and "checkout" with cart and checkout page slug if needed
if ( is_page( 'cart' ) ) {
wp_redirect( '/checkout/' );
die();
}
} );
Step 3: Redirect empty Woocommerce checkout page to shop page
Now, this part is needed only if you added a redirection from Woocommerce cart page to checkout page in the step 2. If you didn’t do it then you can skip this part.
BUT if you are redirecting your cart page then this part is crucial because otherwise you will end up with “Too many redirects” error on your checkout page after deleting the products from cart.
Take a look at the “shop” slug inside the code. Replace it with your own shop page slug.
// Redirect to home url from empty Woocommerce checkout page
add_action( 'template_redirect', 'redirect_empty_checkout' );
function redirect_empty_checkout() {
if ( is_checkout() && 0 == WC()->cart->get_cart_contents_count() && ! is_wc_endpoint_url( 'order-pay' ) && ! is_wc_endpoint_url( 'order-received' ) ) {
wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit;
}
}
Alternative option: Show “Return to shop” button in empty checkout page
If you don’t want to redirect your customer to the shop page and want to show “Return to shop” button instead, then skip the previous snippet and use this one here instead.
add_filter( 'woocommerce_checkout_redirect_empty_cart', '__return_false' );
add_filter( 'woocommerce_checkout_update_order_review_expired', '__return_false' );
Step 4: Automatically remove out of stock products from cart
This code checks each item in the cart and automatically removes it if it’s out of stock. It also displays an error notice to the user to let them know that one or more items have been removed. NB! This code works only if Woocommerce cart and checkout pages are merged. t woesn’t work for default Woocommerce setup. See next snippet for that.
// Automatically remove Woocommerce out of stock products from cart
add_action( 'woocommerce_check_cart_items', 'wpsh_remove_from_cart' );
function wpsh_remove_from_cart() {
global $woocommerce;
$refresh_count = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = $cart_item['data'];
if ( ! $_product->is_in_stock() ) {
$woocommerce->cart->remove_cart_item( $cart_item_key );
wc_add_notice( __( 'One or more items in your cart are out of stock and have been removed.', 'woocommerce' ), 'error' );
$refresh_count++;
}
}
if ( $refresh_count > 0 ) {
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
If you would like to remove out of stock products for default Woocommerce setup, then use this snippet instead. NB! This doesn’t work for merged cart and checkout pages. See the previous snippet for that.
add_action( 'woocommerce_check_cart_items', 'wpsh_remove_out_of_stock_products_from_cart' );
function wpsh_remove_out_of_stock_products_from_cart() {
global $woocommerce;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = $cart_item['data'];
if ( ! $_product->is_in_stock() ) {
$woocommerce->cart->remove_cart_item( $cart_item_key );
wc_clear_notices();
wc_add_notice( __( 'One or more items in your cart are out of stock and have been removed.', 'woocommerce' ), 'error' );
}
}
}
Step 5: Customize Woocommerce checkout page
This part here is again optional. I will do that because I am using a Blocksy theme and it shows me the cart table in a 2/3 with. I would like to show it in a full with. So, all these CSS snippets go to the Customizer >> Additional CSS and applies only for Blocksy theme.
Blocksy theme
SAVE 10% COUPON: WPSH10
Kadence theme
SAVE 10% COUPON: SIMPLEHACKS
This will make the cart form full width.
@media only screen and (min-width: 1000px) {
.ct-cart-form {
grid-template-columns: 1fr;
}
Hide Woocommerce coupon toggle from checkout page
Both cart table and checkout table have their own coupon code box. Since I need only one of them I’ll hide checkout page coupon toggle (“Have a coupon? Click here to enter your code”)
.woocommerce-form-coupon-toggle
{
display: none;
}
This should work with every decent theme.
Customize Woocommerce checkout order review table
Now I’ll add a background to the Woocommerce order review table, add some spacing between this and cart table and make some other adjustments. All classes with ct- is for Blocksy theme only. Two last ones should work with every theme.
/* Blocksy theme order review table background and border */
.ct-order-review {
background: #fbfbfb;
border: 2px dashed #ebebeb;
}
.ct-cart-form {
margin-bottom: 2em;
border-bottom: 1px solid #ebebeb;
}
/* Payment methods background + radio and checkbox customization */
.payment_methods>li:not(.woocommerce-notice),
.woocommerce-form__input[type="checkbox"]{
background: #fff;
}
/* Payment methods background */
.woocommerce-shipping-totals input[type="radio"] {
border: 1px solid #000;
background: #fff;
}
To sum up: It is easy to add Woocommerce cart page to checkout page
As you saw it is fairly easy to merge Woocommerce cart page and checkout page. It will most likely take you 5-10 minutes to accomplish all that.