phpwordpresswoocommercecheckout

Show message based on cart total before order review table in WooCommerce checkout


On my WooCommerce checkout page, I am trying to show a notification before the order review section when the total of the cart equals €0.01

So far, I have placed the following in the functions.php file of my child theme:

add_action('woocommerce_checkout_before_order_review', 'test_funtion');
function test_funtion(){
    ?>
        <p>Notice goes here</p>
    <?php
}

How can I modify these lines such that this notice is only shown if the order total equals € 0.01?


Solution

  • You can use WC_Cart::get_cart_contents_total(); - Gets cart total. This is the total of items in the cart, but after discounts. Subtotal is before discounts.

    So you get:

    function action_woocommerce_checkout_before_order_review () {
        // Get cart total
        $cart_total = WC()->cart->get_cart_contents_total();
        
        // Compare
        if ( $cart_total == 0.01 ) {
            echo '<p>' . __( 'My message', 'woocommerce' ) . '</p>';
        }
    }
    add_action( 'woocommerce_checkout_before_order_review', 'action_woocommerce_checkout_before_order_review', 10, 0 );