Skip to content
Advertisement

Adding custom text after order total in Woocommerce orders and emails

I am using this to display a custom text for customers from specific countries on the cart and checkout page:

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;
}

This does however NOT add the custom text after the order totals in the plain order emails that Woocommerce is sending. I know there is a filter woocommerce_get_formatted_order_total but I cannot seem to get a working function with this. How can I modify my function above to also display the custom text after the price in the plain order emails?

Advertisement

Answer

For displaying this custom text in WooCommerce orders and emails after total, use the following:

add_filter( 'woocommerce_get_order_item_totals', 'custom_order_total_message_html', 10, 3 );
function custom_order_total_message_html( $total_rows, $order, $tax_display ) {
    if( in_array( $order->get_shipping_country(), array('US', 'CA') ) && isset($total_rows['order_total']) ) {
        $total_rows['order_total']['value'] .= ' <small>' . __('My text.', 'woocommerce') . '</small>';
    }
    return $total_rows;
}

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


To make it work only on email notifications use:

add_filter( 'woocommerce_get_order_item_totals', 'custom_order_total_message_html', 10, 3 );
function custom_order_total_message_html( $total_rows, $order, $tax_display ) {
    if( in_array( $order->get_shipping_country(), array('US', 'CA') ) && isset($total_rows['order_total']) && ! is_wc_endpoint_url() ) {
        $total_rows['order_total']['value'] .= ' <small>' . __('My text.', 'woocommerce') . '</small>';
    }
    return $total_rows;
}

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

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement