WooCommerce 中特定付款方式的自定义 class 结帐提交按钮文本
Custom class checkout submit button text for a specific payment method in WooCommerce
我需要为特定支付网关 (bacs) 自定义 class order_button_text。
这是我的函数:
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('bacs',$available_gateways)) {
// Gateway ID for Bacs is 'bacs'.
$available_gateways['bacs']->order_button_text = __( 'Order Pay', 'woocommerce' );
}
return $available_gateways;
}
我不会在:->order_button_text = __( 'Order Pay', 'woocommerce');
我看第二个函数:
add_filter( 'woocommerce_order_button_html', 'misha_custom_button_html' );
function misha_custom_button_html( $button_html ) {
$order_button_text = 'Submit';
$button_html = '<button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '">' . esc_html( $order_button_text ) . '</button>';
}
我试图仅在付款方式 (bacs) 的情况下集成第二个功能
您可以通过 javascript
完成此操作。您可以查看 payment_method
单选按钮的更改。
Get selected payment method using :checked
.
var payment_method = $('input[name=payment_method]:checked').val();
You can also check on page load which payment method is selected using
the updated_checkout
javascript trigger.
$( 'body' ).on( 'updated_checkout',function( data ){
console.log( data );
} );
Use the change
event to get the selected payment method.
$(document).on( 'change', 'input[name="payment_method"]', function(){
console.log( $(this).val() );
});
检查下面的代码。代码进入您的活动主题 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 );
已测试并有效(抱歉质量低)
我需要为特定支付网关 (bacs) 自定义 class order_button_text。
这是我的函数:
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('bacs',$available_gateways)) {
// Gateway ID for Bacs is 'bacs'.
$available_gateways['bacs']->order_button_text = __( 'Order Pay', 'woocommerce' );
}
return $available_gateways;
}
我不会在:->order_button_text = __( 'Order Pay', 'woocommerce');
我看第二个函数:
add_filter( 'woocommerce_order_button_html', 'misha_custom_button_html' );
function misha_custom_button_html( $button_html ) {
$order_button_text = 'Submit';
$button_html = '<button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '">' . esc_html( $order_button_text ) . '</button>';
}
我试图仅在付款方式 (bacs) 的情况下集成第二个功能
您可以通过 javascript
完成此操作。您可以查看 payment_method
单选按钮的更改。
Get selected payment method using
:checked
.
var payment_method = $('input[name=payment_method]:checked').val();
You can also check on page load which payment method is selected using the
updated_checkout
javascript trigger.
$( 'body' ).on( 'updated_checkout',function( data ){
console.log( data );
} );
Use the
change
event to get the selected payment method.
$(document).on( 'change', 'input[name="payment_method"]', function(){
console.log( $(this).val() );
});
检查下面的代码。代码进入您的活动主题 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 );
已测试并有效(抱歉质量低)