更改 WooCommerce 中特定付款方式的结帐提交按钮文本

Change checkout submit button text for a specific payment method in WooCommerce

我需要更改特定支付网关(在本例中为 COD)的 order_button_text

我只能让它在全球范围内改变(对于所有支付网关)使用:

add_action( 'woocommerce_after_add_to_cart_button', 'multiple_orders_text' );
function woo_custom_order_button_text() {
    return __( 'Request Shipping Quote', 'woocommerce' );
}

但是发现如果我添加行

$this->order_button_text  = __( 'Request a Quote', 'woocommerce' );

woocommerce/includes/gateways/cod/class-wc-gateway-cod.php 中的 setup_properties() 方法确实有效。

然而,这显然是不好的做法,因为我正在破解核心插件文件。

如何在不破解 woocommerce 核心文件的情况下实现这一目标?

这是最简洁的方法,使用 woocommerce_review_order_before_payment 动作钩子和自定义函数,主要使用 jQuery (因为这是客户端现场活动):

add_action( 'woocommerce_review_order_before_payment', 'customizing_checkout_button', 10, 0 );
function customizing_checkout_button(){

    $text1  = __( 'Place order', 'woocommerce' );
    $text2  = __( 'Request a Quote', 'woocommerce' );

    ?>
    <script>
        jQuery(function($){

            // 1. Initialising once loaded
            if($('input[name^="payment_method"]:checked').val() == 'cod' )
                $('input[name^="woocommerce_checkout_place_order"]').val('<?php echo $text2; ?>');
            else
                $('input[name^="woocommerce_checkout_place_order"]').val('<?php echo $text1; ?>');

            // 2. Live event detection:When payment method is changed
            $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
                var choosenPaymentMethod = $('input[name^="payment_method"]:checked').val(); // Chosen

                if( choosenPaymentMethod == 'cod' )
                    $('input[name^="woocommerce_checkout_place_order"]').val('<?php echo $text2; ?>');
                else 
                    $('input[name^="woocommerce_checkout_place_order"]').val('<?php echo $text1; ?>');
            });
        });
    </script>
    <?php
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

经过测试并可与 WooCommerce 3+ 一起使用

你可以这样做:

add_filter( 'woocommerce_available_payment_gateways', 'woocommerce_available_payment_gateways' );
function woocommerce_available_payment_gateways( $available_gateways ) {
    if (! is_checkout() ) return $available_gateways;  // stop doing anything if we're not on checkout page.
    if (array_key_exists('paypal',$available_gateways)) {
        // Gateway ID for Paypal is 'paypal'. 
         $available_gateways['paypal']->order_button_text = __( 'Request a Quote', 'woocommerce' );
    }
    return $available_gateways;
}

此代码示例适用于贝宝。有关网关 ID 的参考,请查看 WooCoomerce > Settings > Checkout > 网关显示顺序

简单的解决方案,尝试在主题的 function.php 文件中添加以下代码。

/**
 * @snippet       Change checkout order button text
 * @package       WooCommerce
 */
function change_checkout_order_button_text() {
    return __( 'Complete Order', 'woocommerce' ); 
}
add_filter( 'woocommerce_order_button_text', 'change_checkout_order_button_text' );

我的网站上有两种付款方式。现金或卡支付

我正在尝试更改 woocommerce 上“下订单”按钮的文本。

如果付款方式是现金,我希望按钮显示“确认”

如果付款方式是信用卡,我希望按钮显示“确认并付款”

我正在使用以下方法将文本更改为确认,但需要一些帮助以根据付款方式添加额外条件:

add_filter( 'woocommerce_order_button_text', 'woo_custom_order_button_text' ); 

function woo_custom_order_button_text() {
    return __( 'Confirm', 'woocommerce' ); 
}