How to customize Woocommerce cart page? 23 useful Woocommerce Cart Page Hacks

In my previous posts (see below) I showed how to hack single product pages and category pages. Today I’m going to show you 21 useful Woocommerce cart page hacks. Now, before I start I have to point out that all those snippets go to your child theme’s functions.php file. Or better yet – use Code Snippets plugin for it.

WpCodeBox is my favorite code snippets manager for WordPress. This is a premium plugin and if you’re interested, then grab WPCodeBox with a nice 20% discount here (SAVE 20% Coupon WPSH20).

So, let’s dive in.

Video: How to customize Woocommerce cart page?

If you’re new to the Woocommerce or using hooks and snippets then it would be wise for you to see the video here below. In it I’ll show you how to add those hacks and how will they look like on a live site.

How to add custom content to Woocommerce empty cart page?

By default Woocommerce shows “Your cart is currently empty” message. This snippet here below adds “You haven’t added any products to the cart yet. Although, you may be interested in these products.” text along with the featured products display.

If you see the snippet then you can add whatever shortcode or text you want. Just replace (or remove) shortcode and text as you like.

<span role="button" tabindex="0" data-code="// Adds custom content to Woocommerce empty cart page?
add_action( 'woocommerce_cart_is_empty', 'empty_cart_custom_content' );
function empty_cart_custom_content() {
echo '<h4>You haven’t added any products to the cart yet. Although, you may be interested in these products.

// Adds custom content to Woocommerce empty cart page?
add_action( 'woocommerce_cart_is_empty', 'empty_cart_custom_content' );
function empty_cart_custom_content() {
  echo '<h4>You haven’t added any products to the cart yet. Although, you may be interested in these products.</h4>';
  echo do_shortcode('
'
);
}

How to set Minimum Order Amount in WooCommerce?

With the help of this snippet here below we will set a minimum order amount in Woocommerce to 1000 euros and will display an error message on the cart and checkout pages if the conditions are not met (see the screenshot). Just replace the amount inside the code accordingly.

<span role="button" tabindex="0" data-code="// Set Minimum Order Amount in WooCommerce
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );

function wc_minimum_order_amount() {

$minimum = 1000; // Set this variable to specify a minimum order value

if ( WC()->cart->total

// Set Minimum Order Amount in WooCommerce
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
 
function wc_minimum_order_amount() {
    
    $minimum = 1000; // Set this variable to specify a minimum order value

    if ( WC()->cart->total < $minimum ) {

        if( is_cart() ) {
            wc_print_notice( 
                sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order ' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );
        } else {
            wc_add_notice( 
                sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );

        }
    }
}

How to show “XX to free shipping” notification in Woocommerce?

It is a good idea to motivate your users to buy a bit more in order to get free shipping. This snippet here below will add this text to your cart “Buy XX€ worth products more to get free shipping”

How to show "XX to free shipping" notification in Woocommerce?
<span role="button" tabindex="0" data-code="// Show "XX to free shipping" notification in Woocommerce
add_action( 'woocommerce_before_cart_table', 'cart_page_notice' );

function cart_page_notice() {
$min_amount = 1000; //This is the amount of your free shipping threshold. Change according to your free shipping settings
$current = WC()->cart->subtotal;
if ( $current < $min_amount ) {
$added_text = '<div class="woocommerce-message"><strong>Buy ' . wc_price( $min_amount – $current ) . ' worth products more to get free shipping</strong>'; // This is the message shown on the cart page
$return_to = wc_get_page_permalink( 'shop' );
$notice = sprintf( '%s<a class="button" href="%s">%s</a>', $added_text, esc_url( $return_to ), 'Continue shopping

// Show "XX to free shipping" notification in Woocommerce
add_action( 'woocommerce_before_cart_table', 'cart_page_notice' );
 
function cart_page_notice() {
	$min_amount = 1000; //This is the amount of your free shipping threshold. Change according to your free shipping settings
	$current = WC()->cart->subtotal;
	if ( $current < $min_amount ) {
	$added_text = '<div class="woocommerce-message"><strong>Buy  ' . wc_price( $min_amount - $current ) . ' worth products more to get free shipping</strong>'; // This is the message shown on the cart page
	$return_to = wc_get_page_permalink( 'shop' );
	$notice = sprintf( '%s<a class="button" href="%s">%s</a>', $added_text, esc_url( $return_to ), 'Continue shopping</div>' ); // This is the text shown below the notification. Link redirects to the shop page
	echo $notice;
	}
}

How to display custom message to Woocommerce cart page?

Maybe you need to warn your customers that shipping is delayed or you need something elso to communicate. If so, then use this snippet and replace text accordingly.

How to add custom message to Woocommerce cart page?
<span role="button" tabindex="0" data-code="// Add custom message to Woocommerce cart page
add_action( 'woocommerce_before_cart_table', 'shop_message', 20 );
function shop_message() {
echo '<p class="woocommerce-message">Estimated delivery time: 2 weeks

// Add custom message to Woocommerce cart page
add_action( 'woocommerce_before_cart_table', 'shop_message', 20 );
function shop_message() {
echo '<p class="woocommerce-message">Estimated delivery time: 2 weeks</p>'; // Change this text
}

How to display a custom message in Woocommerce cart page for specific products?

If previous snippet displays custom message for all the products in cart then this one here will display custom message in Woocommerce cart page for specific products. For example, if product “T-shirt ABC” is added to the cart, only then this message is displayed.

PS! Take a look at the line 4 – this contains your product ID numbers. Also, change the message in line 10 accordingly.

// Display a custom message in Woocommerce cart page for specific products
add_action( 'woocommerce_before_cart', 'wpsh_cart_message_for_specific_product' );
function wpsh_cart_message_for_specific_product() {
    $product_ids = array(191, 24); // Add your product ID numbers here

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $item ) {
        if ( array_intersect( $product_ids, array($item['product_id'], $item['variation_id']) ) ) {
            // This is the message displayed in cart
            wc_print_notice( __("Estimated delivery time: 2 weeks"), 'notice' );
            break; // Stop the loop
        }
    }
}

How to add Woocommerce backorder notification in cart page?

I have had customers who complain that they did not realize that they ordered a product which was not in stock and was available on backorder. Therefore, this snippets adds Woocommerce backorder notification in cart page.

As you see from the screenshot below it outputs this message styled as error message. If you want it to show as default Woocommerce message notification then replace ‘error’ with ‘notice’

How to add Woocommerce backorder notification in cart page?
<span role="button" tabindex="0" data-code="// Add Woocommerce backorder notification in cart page
add_action( 'woocommerce_before_cart_table', 'show_backordered_items_cart_notice' );
function show_backordered_items_cart_notice() {
$found = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
$found = true;
break;
}
}
if( $found ) {
// Change this text here. If you want it to show as default Woocommerce message notification then replace 'error' with 'notice'
wc_print_notice( __("<strong>You have products in the cart that are available only in backorder.</strong>

// Add Woocommerce backorder notification in cart page
add_action( 'woocommerce_before_cart_table', 'show_backordered_items_cart_notice' );
function show_backordered_items_cart_notice() {
    $found = false;
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
            $found = true;
            break;
        }
    }
    if( $found ) {
// Change this text here. If you want it to show as default Woocommerce message notification then replace 'error' with 'notice'
        wc_print_notice( __("<strong>You have products in the cart that are available only in backorder.</strong><br> For those products estimated delivery time is 2-3 weeks.", "woocommerce"), 'error' ); 
    }
}

How to Update WooCommerce Cart on Quantity Change?

Next snippet is also a nice one. This allows you to hide Update cart button and it will update your cart amount every time you update product quantities. So, if you need to update WooCommerce cart on quantity change then use this snippet here below.

<span role="button" tabindex="0" data-code="// Update WooCommerce Cart on Quantity Change

add_action('wp_head', function(){

$css_code = <<<CSS

  • How to hide company field based on a Woocommerce custom checkout radio selection?

  • How to use Woocommerce coupons in 23 different ways? (23 simple hacks)

  • How to Customize Woocommerce Orders page? (20 hacks)

  • How to customize Woocommerce cart page? 21 useful Woocommerce Cart Page Hacks

  • How to Customize Woocommerce shop and category page? 17 useful hacks

  • How to customize Woocommerce Single Product Page | 14 Useful Hacks

  • How to Customize Woocommerce Checkout page? 28 useful hacks

  • How to customize Woocommerce Single Product Page | 14 Useful Hacks

  • How to Hide Woocommerce Shipping Methods (Conditionally) – 15 Hacks

  • How to Customize Woocommerce Stock Status? (17 hacks)

Leave a Comment

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

Scroll to Top