If Cart Total Above Specific Amount Display Dynamic Notice

This tutorial contains 2 different ways to show text, a message or a notice based on the cart total in WooCommerce.

The 1st solution displays text within the cart totals table :

Add the following PHP code to the end of your child themes functions file.

add_action('woocommerce_cart_calculate_fees', 'apply_shipping_fee_based_on_cart_total', 10, 1);
function apply_shipping_fee_based_on_cart_total($cart) {

    if ( is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    $cart_total = WC()->cart->get_subtotal();

    if ( $cart_total < 100 ) {
        WC()->cart->add_fee(__('Orders Above $100 Get Free Shipping', 'woocommerce'), 9);
    } 
    
}

The 2nd solution uses jQuery to check the cart totals and dynamically display a notice when the cart subtotals reach a set limit without refreshing the cart page.

You can set the limit for the cart total on line 17 of notice.js.

There’s 2 steps required to install the code from the download folder for the 2nd solution.

  1. Upload the file named notice.js to your child themes folder.
  2. Copy and paste the PHP code from the functions.php file to the end of your child themes functions file.

This solution is coded to work with the cart block however it will also work with non block themes if you add a div where you want the notice to display using code like this in your child themes functions file:

add_action('woocommerce_before_cart', 'hook_cart_notice');
function hook_cart_notice() {
    echo '<div id="cart-notice"></div>';
}

The code for the notice is located on line 21 for themes using the block cart and line 22 for themes not using blocks.

Was This Tutorial Helpful?

Free

$0

Access only to all free tutorials per month.



Monthly

$75

Access to 10 premium tutorials per month.


Tutorial Request


Includes code guarantee and coding support.

Yearly

$500

Access to 15 premium tutorials per month.


Monthly Tutorial Request


Includes code guarantee and priority coding support.