仅在Woocommerce结帐页面中将文本附加到总价格

问题描述 投票:3回答:1

我有以下代码在BOTH购物车和结帐页面的总部分添加后缀文本:

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

但是,我只希望后缀文本仅在结帐时显示,而不是在购物车页面中显示。

我该如何做到这一点?

php wordpress woocommerce cart checkout
1个回答
3
投票

只需使用Woocommerce conditional tags来限制结账页面上的显示...

现在你应该更好地使用下面的钩子,以避免出现问题,用总量上的叮咬来熔化浮点数:

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

或者在总计之后的单独表格行上更好,使用此代替:

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
}

代码在您的活动子主题(或主题)的function.php文件中。经过测试和工作。


如果您决定保留初始挂钩,请使用以下命令:

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

代码在您的活动子主题(或主题)的function.php文件中。未经测试。

© www.soinside.com 2019 - 2024. All rights reserved.