【问题标题】:Append a text to total price only in Woocommerce checkout page仅在 Woocommerce 结帐页面中将文本附加到总价
【发布时间】:2019-08-10 13:10:00
【问题描述】:

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

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

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

我该如何做到这一点?

【问题讨论】:

  • 我会避免以这种方式修改价格,也许像add_action( 'woocommerce_after_order_notes', 'custom_total_message') 这样的东西会更合适。我以前用它来处理类似“请允许 14 天的订单处理”类型的消息。

标签: php wordpress woocommerce cart checkout


【解决方案1】:

只需使用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 文件中。未经测试。

【讨论】:

猜你喜欢
  • 2017-02-15
  • 2017-01-20
  • 2014-08-17
  • 2015-11-29
  • 2017-10-08
  • 2014-04-24
  • 2015-01-17
  • 2019-11-13
  • 1970-01-01
相关资源
最近更新 更多