Wordpress WooCommerce中特定付款方式的自定义类签出提交按钮文本

Wordpress WooCommerce中特定付款方式的自定义类签出提交按钮文本,wordpress,hook,payment-gateway,hook-woocommerce,Wordpress,Hook,Payment Gateway,Hook Woocommerce,我需要为特定支付网关(bacs)的订单按钮文本定制类 这是我的职责: add_filter('woocmerce_available_payment_gateways'、'woocmerce_available_payment_gateways'); 功能商业可用支付网关($available\u gateways){ 如果(!is_checkout())返回$available_gateways;//如果我们不在签出页面上,请停止执行任何操作。 如果(存在阵列密钥('bacs',$可用网关)

我需要为特定支付网关(bacs)的订单按钮文本定制类

这是我的职责:

add_filter('woocmerce_available_payment_gateways'、'woocmerce_available_payment_gateways');
功能商业可用支付网关($available\u gateways){
如果(!is_checkout())返回$available_gateways;//如果我们不在签出页面上,请停止执行任何操作。
如果(存在阵列密钥('bacs',$可用网关)){
//Bacs的网关ID为“Bacs”。
$available_gateways['bacs']->订单按钮_文本=uuuuu('order Pay','woocommerce');
}
返回$available\u网关;

}
您可以通过
javascript
实现这一点。您可以通过更改单选按钮来检查付款方式

使用
获取所选付款方式:选中

您还可以在页面加载时检查使用的付款方式
updated\u checkout
javascript触发器

使用
change
事件获取所选的付款方式

检查下面的代码。代码放入活动的theme functions.php文件中

function add_checkout_js(){
    ?>
    <script type="text/javascript">
        (function($){
            
            var payment_method = $('input[name=payment_method]:checked').val();
            
            ChnageButtonTextBasedOnPaymentMethod( payment_method );

            $( 'body' ).on( 'updated_checkout',function( data ){
                ChnageButtonTextBasedOnPaymentMethod( payment_method );
            } );
            
            $(document).on( 'change', 'input[name="payment_method"]', function(){
                ChnageButtonTextBasedOnPaymentMethod( $(this).val() );
            });

            function ChnageButtonTextBasedOnPaymentMethod( payment_method ){
                if( payment_method == 'bacs' ){
                    $('#place_order').addClass('custom-class');
                    $('#place_order').val('Order Pay');
                    $('#place_order').attr('data-value','Order Pay');
                    $('#place_order').html('Order Pay <img style="display: unset;" src="">');
                }else{
                    $('#place_order').removeClass('custom-class');
                    $('#place_order').val('Place order');
                    $('#place_order').attr('data-value','Place order');
                    $('#place_order').text('Place order');
                }
            }
            
        })(jQuery);
    </script>
    <?php
}

add_action( 'wp_footer', 'add_checkout_js', 10, 1 );
函数add\u checkout\u js(){
?>
(函数($){
var payment_method=$('input[name=payment_method]:checked').val();
chnagebuttonextbasedonpaymentmethod(付款方式);
$('body')。打开('updated_checkout',函数(数据){
chnagebuttonextbasedonpaymentmethod(付款方式);
} );
$(文档).on('change','input[name=“payment_method”]”,function(){
ChnageButtonTextBasedOnPaymentMethod($(this.val());
});
函数ChnageButtonTextBasedOnPaymentMethod(付款方法){
如果(付款方式=='bacs'){
$(“#下订单”).addClass('custom-class');
$('place#u order').val('order Pay');
$('place#u order').attr('data-value','order Pay');
$(“#下订单”).html(“订单付款”);
}否则{
$(“#下订单”).removeClass('custom-class');
$('下单').val('下单');
$('place'u order').attr('data-value','place-order');
$(“#下订单”)。文本(“下订单”);
}
}
})(jQuery);

谢谢Bhautick先生,你的公式工作得很好,真的很酷。最后一点帮助,你可以在$('#place#u order')之后插入一个图像(svg、png、图标等)。文本('Pay with');是的,我更新了我的答案。-------类似以下代码:$(document.body)。on('payment_method_selected',function(){var selectedPaymentMethod=$('.woocommerce checkout input[name=“payment_method”]:checked').attr('id');$('#place_order').find('.payment icon');$('#place_order').prepend(';//或类似于字体awome.}的任何元素;$(document.body).触发器('payment#method_selected'));//这将在页面加载时触发,充当初始化图标的角色。提前感谢您的帮助!您使用的是真棒字体吗?我添加了一个图像标记,您现在可以在其中添加图像源。
$( 'body' ).on( 'updated_checkout',function( data ){
    console.log( data );
} );
$(document).on( 'change', 'input[name="payment_method"]', function(){
    console.log( $(this).val() );
});
function add_checkout_js(){
    ?>
    <script type="text/javascript">
        (function($){
            
            var payment_method = $('input[name=payment_method]:checked').val();
            
            ChnageButtonTextBasedOnPaymentMethod( payment_method );

            $( 'body' ).on( 'updated_checkout',function( data ){
                ChnageButtonTextBasedOnPaymentMethod( payment_method );
            } );
            
            $(document).on( 'change', 'input[name="payment_method"]', function(){
                ChnageButtonTextBasedOnPaymentMethod( $(this).val() );
            });

            function ChnageButtonTextBasedOnPaymentMethod( payment_method ){
                if( payment_method == 'bacs' ){
                    $('#place_order').addClass('custom-class');
                    $('#place_order').val('Order Pay');
                    $('#place_order').attr('data-value','Order Pay');
                    $('#place_order').html('Order Pay <img style="display: unset;" src="">');
                }else{
                    $('#place_order').removeClass('custom-class');
                    $('#place_order').val('Place order');
                    $('#place_order').attr('data-value','Place order');
                    $('#place_order').text('Place order');
                }
            }
            
        })(jQuery);
    </script>
    <?php
}

add_action( 'wp_footer', 'add_checkout_js', 10, 1 );