Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append a text to total price only in Woocommerce checkout page

I have the following code that adds suffix text in the total section of BOTH cart and checkout pages:

add_filter( 'woocommerce_cart_total', 'custom_total_message' );
function custom_total_message( $price ) {
    $msg = 'Prices for grocery items may vary at store. Final bill will be based on store receipt.<br />';

    return $price . $msg;
}

However, I only want the suffix text to display ONLY in the checkout but not cart page.

How do I accomplish this?

like image 557
Les Avatar asked Mar 19 '19 22:03

Les


1 Answers

Simply use Woocommerce conditional tags to restrict the display on checkout page only…

Now you should better use the following hook instead, to avoid problems, melting float numbers with stings on total amount:

add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_total_message_html', 10, 1 );
function custom_total_message_html( $value ) {
    if( is_checkout() )
        $value .= __('Prices for grocery items may vary at store. Final bill will be based on store receipt.') . '<br />';

    return $value;
}

Or even better on a separate table row after the total, using this instead:

add_action( 'woocommerce_review_order_after_order_total', 'review_order_after_order_total_callback' );
function review_order_after_order_total_callback(){
    $text = __('Prices for grocery items may vary at store. Final bill will be based on store receipt.');

    ?><tr class="order-total"><th colspan="2"><?php echo $text; ?></th></tr><?php
}

Code goes on function.php file of your active child theme (or theme). Tested and works.


If you decide to keep your initial hook, use the following:

add_filter( 'woocommerce_cart_total', 'custom_total_message', 10, 1 );
function custom_total_message( $price ) {
    if( is_checkout() )
        $price .= __('Prices for grocery items may vary at store. Final bill will be based on store receipt.') . '<br />';

    return $price;
}

Code goes on function.php file of your active child theme (or theme). Untested.

like image 146
LoicTheAztec Avatar answered Nov 09 '22 00:11

LoicTheAztec