【问题标题】:Add an additional message to the "Thank you" page in Woocommerce 3向 Woocommerce 3 中的“谢谢”页面添加附加消息
【发布时间】:2019-01-04 13:52:23
【问题描述】:

需要在“谢谢。您的订单已收到。”之后添加自定义消息。 我的消息中添加了一个变量。它包含订单总额的百分比。

以下代码有效,但我不确定:

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

这是更好的方法吗?如何增强我的代码?

【问题讨论】:

    标签: php wordpress woocommerce checkout hook-woocommerce


    【解决方案1】:
    add_action( 'woocommerce_thankyou', 'my_custom_thankyou_page', 20 );
    function my_custom_thankyou_page( $order_id ){  
        $order = wc_get_order($order_id);
        $order_total = $order->get_total();
        $percent = get_option( 'wc-custom-percent' ); // Percentage
        $order_saving = (float)($percent * $order_total / 100); // Bonus amount
    
        $new_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;
    
    }
    

    【讨论】:

      【解决方案2】:

      您的代码是正确的,但您可以使用sprintf() 和可翻译的文本使其更好一些:

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

      代码进入活动子主题(或活动主题)的 function.php 文件中。经过测试并且有效。

      【讨论】:

        猜你喜欢
        • 2021-10-16
        • 1970-01-01
        • 2016-06-18
        • 1970-01-01
        • 2021-09-12
        • 1970-01-01
        • 2019-09-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多