我已经试了好几个小时了,但无论如何我都不能让这个"基本"的东西工作.

我有一堆支付网关可用,我需要它的名称,包括总订单价值,以包括在"立即付款"按钮文本.

Example:"Order & Pay $49 using Stripe."

我的代码应该会在网关更改时自动更新 checkout .拜托了,有人吗?

add_filter( 'woocommerce_order_button_text', 'order_button_text_based_on_gateway', 10, 1 );
function order_button_text_based_on_gateway( $cart ) {

    // make sure we get the payment gateways
    $payment_method = WC()->session->get( 'chosen_payment_method' );

    // based on different gateways, display a different button text (place order button)
    if ( $payment_method == ' bacs ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' using WireTransfer' );
        } 
    
    elseif ( $payment_method == ' cheque ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' with a personal cheque' );
        } 

    elseif ( $payment_method == ' cod ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' on delivery' );
        }

    elseif ( $payment_method == ' etco ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' using EtCo' );
        }

    else ( $payment_method == ' stripe ' ) {

            return sprintf( '%s %s', __('Order & Pay', 'woocommerce'), 
            strip_tags( WC()->cart->get_total() ) . ' using Stripe' );
        }

}

The "auto update" checkout script:

add_action( 'wp_footer', 'reload_checkout_based_on_gateway_change', 999 ); 
function reload_checkout_based_on_gateway_change() {

    if ( is_checkout() && ! is_admin() ) {

    // close PHP, get the SCRIPT going
    ?>

        <script>
        ( function( $ ) {
        $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {

                $( 'body' ).trigger( 'update_checkout' );
            }
        );
      }
    )
    
     ( jQuery );

        </script>
    
        <?php
    }
}

推荐答案

您的代码中有很多错误:

  • 主要的错误是关于字符串:' cheque ''cheque'是两个不同的字符串.
    因此,在您的所有IF对帐单中,没有匹配的付款方式.
  • 另一个是else不支持任何条件参数.

有多种方法可以更改 checkout "下单"按钮文本:

add_filter( 'woocommerce_order_button_text', 'order_button_text_based_on_gateway', 10 );
function order_button_text_based_on_gateway( $button_text ) {
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        $payment_method    = WC()->session->get( 'chosen_payment_method' ); // Get current payment gateways
        $cart_total_string = strip_tags( WC()->cart->get_total() ); // Get order total string
        $pay_order_text    = __('Order &amp; Pay', 'woocommerce'); // order button

        if ( $payment_method == 'bacs' ) {
            $payment_method_text = __('using WireTransfer', 'woocommerce');
        } 
        elseif ( $payment_method == 'cheque' ) {
            $payment_method_text = __('with a personal cheque', 'woocommerce');
        } 
        elseif ( $payment_method == 'cod' ) {
            $payment_method_text = __('on delivery', 'woocommerce');
        }
        elseif ( $payment_method == 'etco' ) {
            $payment_method_text = __('using EtCo', 'woocommerce');
        }
        elseif ( $payment_method == 'stripe' ) {
            $payment_method_text = __('using Stripe', 'woocommerce');
        }

        if ( isset($payment_method_text) ) {
            $button_text = sprintf( '%s %s %s', $pay_order_text, $cart_total_string, $payment_method_text );
        }
    }
    return $button_text;
}

// Update checkout on payment method change (jQuery)
add_action( 'woocommerce_checkout_init', 'trigger_update_checkout_on_payment_method_change' );
function trigger_update_checkout_on_payment_method_change(){
    wc_enqueue_js("$('form.checkout').on( 'change', 'input[name=payment_method]', function(){
        $(document.body).trigger('update_checkout');
    });");
}

也可以使用WC_Payment_Gateway order_button_text属性,如:

add_filter('woocommerce_available_payment_gateways', 'change_payment_text_button');
function change_payment_text_button( $payment_gateways ) {
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        $cart_total_string = strip_tags( WC()->cart->get_total() ); // Get order total string
        $pay_order_text    = __('Order &amp; Pay', 'woocommerce'); // order button text

        if ( isset($payment_gateways['bacs']) ) {
            $payment_gateways['bacs']->order_button_text = sprintf( '%s %s %s', 
                $pay_order_text, $cart_total_string, __('using WireTransfer', 'woocommerce') );
        } 
        if ( isset($payment_gateways['cheque']) ) {
            $payment_gateways['cheque']->order_button_text = sprintf( '%s %s %s', 
                $pay_order_text, $cart_total_string, __('with a personal cheque', 'woocommerce') );
        } 
        if ( isset($payment_gateways['cod']) ) {
            $payment_gateways['cod']->order_button_text = sprintf( '%s %s %s', 
                $pay_order_text, $cart_total_string, __('on delivery', 'woocommerce') );
        }
        if ( isset($payment_gateways['etco']) ) {
            $payment_gateways['etco']->order_button_text = sprintf( '%s %s %s', 
                $pay_order_text, $cart_total_string, __('using EtCo', 'woocommerce') );
        }
        if ( isset($payment_gateways['stripe']) ) {
            $payment_gateways['stripe']->order_button_text = sprintf( '%s %s %s', 
                $pay_order_text, $cart_total_string, __('using Stripe', 'woocommerce') );
        }
    }
    return $payment_gateways;
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.

Note:一些第三方支付网关不允许通过WooCommerce挂钩更改按钮文本.

Php相关问答推荐

Laravel:启动下载并同时在Blade 中显示Flash消息

模型中的Laravel自定义查询字段

WooCommerce产品按特定元数据的自定义数字排序选项

在WooCommerce产品中按类型对产品属性术语进行分类以供显示

PHP原始数据读取引发max_input_vars错误

为什么正则表达式与得到的文本块之前得到的也行?

获取并判断WooCommerce用户订阅结束日期

从WooCommerce订单状态更改更新用户自定义元数据

PHP测试字符串的所有POST值

在Laravel中修改JSON输出

删除了数组中的值,而不是多个数组

基于服务器的不同地平线配置

curl_close()未从PHP 8开始写入CURLOPT_COOKIEJAR会话文件

文本到语音:不考虑音高值

在 Woocommerce 邮箱订单中显示产品 GTIN

订单完成后创建 WooCommerce 促销代码

如何限制 woocommerce 中相关产品的标题长度

ACF 更新字段功能不更新 WordPress 中的任何数据

如何使用在产品快速编辑菜单前端的自定义框中 Select 的值更新 woocommerce 产品属性值?

如何在 php/laravel 中获取元素在数组中的位置