Search code examples
phpwordpresswoocommercecartcountry

Display a text after cart and checkout totals for specific countries in Woocommerce


I need to display a message in the Woocommerce order-total cell after the price, but only if the customer's country is US or Canada. I have this

add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_total_message_html', 10, 1 );
function custom_total_message_html( $value ) {
    $value .= __('<small>My text.</small>');

return $value;
}

How can I only add the text if the customer's country is US or Canada?


Solution

  • To target specific countries in your code, you will use WC_Customer get_shipping_country() method (that use geolocation country for unlogged users), as follows:

    add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_total_message_html', 10, 1 );
    function custom_total_message_html( $value ) {
        if( in_array( WC()->customer->get_shipping_country(), array('US', 'CA') ) ) {
            $value .= '<small>' . __('My text.', 'woocommerce') . '</small>';
        }
        return $value;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.