dongnu4254 2018-07-27 12:26 采纳率: 100%
浏览 55
已采纳

在Woocommerce 3中的“谢谢”页面添加其他消息

Example Need add custom message after "Thank you. Your order has been received.". A variable has been added to my message. It contains the percentage of the total amount of the order.

The following code works but I am not sure about it:

add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );
function woo_change_order_received_text( $str, $order ) {
    // Get order total
    $order_total = $order->get_total();
    $percent = get_option( 'wc-custom-percent' ); // Percentage
    $order_saving = (float)($percent * $order_total / 100); // Bonus amount

    $new_str = $str . ' You participate in the bonus program, your bonus interest from this order is' . $order_saving . ' euros. <a href="#">Learn more about the bonus program.</a>';
    return $new_str;
}

Is it a better way to do it? How can I enhance my code?

  • 写回答

2条回答 默认 最新

  • dongyizhuang0134 2018-07-27 15:06
    关注

    Your code is correct, but you could make it a bit better using sprintf() and translatable texts:

    add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 20, 2 );
    function woo_change_order_received_text( $thankyou_text, $order ) {
        $order_saving = (float)( get_option( 'wc-custom-percent' ) * $order->get_total() / 100 ); // Bonus amount
        $bonus_link   = '#';
    
        return sprintf( __("%s You participate in the bonus program, your bonus interest from this order is %s. %s", "woocommerce"),
        $thankyou_text,
        wc_price($order_saving),
        '<a href="'.$bonus_link.'">' . __("Learn more about the bonus program.", "woocommerce") . '</a>' );
    }
    

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

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了