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

时间:2017-08-17 15:31:43

标签: php jquery wordpress woocommerce checkout

我需要更改特定支付网关的order_button_text(在这种情况下为COD)。

我只能使用以下方式全局更改(适用于所有支付网关)

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

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

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

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

4 个答案:

答案 0 :(得分: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;
}

reigelgallarde.me 这个代码示例适用于paypal。有关网关ID的参考,请检查 WooCoomerce > 设置> 结帐> 网关显示顺序

reigelgallarde.me

答案 1 :(得分:1)

这是干净的方法,使用 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 +一起使用

答案 2 :(得分:0)

简便的解决方案,请尝试在主题的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' );

答案 3 :(得分:0)

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

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

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

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

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

public static char[][] createMatrix(String num, String title) {
    int totalRows = (int)Math.ceil((double)num.length() / title.length());
    char[][] matrix = new char[totalRows + 1][title.length()];

    for (int i = 0; i < matrix[0].length; i++)
        matrix[0][i] = title.charAt(i);

    for (int i = 0, row = 1, col = 0; i < num.length(); i++) {
        matrix[row][col++] = num.charAt(i);

        if (col == matrix[row].length) {
            row++;
            col = 0;
        }
    }

    return matrix;
}
相关问题