本文介绍了显示基于所选付款方式的隐藏自定义Woocommerce结帐字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我使用下面的代码在结算表单中添加了自定义字段.
Hi I added custom field in billing form using this code below.
add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields)
{
$fields['billing_options'] = array(
'label' => __('If you pay by Invoice. Please add Your Invoice Number Here ', 'woocommerce'), // Add custom field label
'placeholder' => _x('Invoice Number', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('my-css') // add class name
);
return $fields;
}
我有两种付款方式:1.货到付款2. Realex Payments HPP –信用卡.
I have two payment options 1. Cash on delivery 2.Realex Payments HPP – Credit Card.
只有在这样的情况下才可以显示自定义字段.1.选择货到付款作为付款方式.
Is it possible to show custom field only then 1. Cash on delivery selected as payment option.?
谢谢
推荐答案
下面的代码将隐藏自定义可选结帐字段当所选择的支付方法是货到付款( COD") :
// Conditional Show hide checkout fields based on chosen payment methods
add_action( 'wp_footer', 'conditionally_show_hide_billing_custom_field' );
function conditionally_show_hide_billing_custom_field(){
// Only on checkout page
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script>
jQuery(function($){
var a = 'input[name="payment_method"]',
b = a + ':checked',
c = '#billing_options_field'; // The checkout field <p> container selector
// Function that shows or hide checkout fields
function showHide( selector = '', action = 'show' ){
if( action == 'show' )
$(selector).show( 200, function(){
$(this).addClass("validate-required");
});
else
$(selector).hide( 200, function(){
$(this).removeClass("validate-required");
});
$(selector).removeClass("woocommerce-validated");
$(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
}
// Initialising: Hide if choosen payment method is "cod"
if( $(b).val() !== 'cod' )
showHide( c, 'hide' );
else
showHide( c );
// Live event (When payment method is changed): Show or Hide based on "cod"
$( 'form.checkout' ).on( 'change', a, function() {
if( $(b).val() !== 'cod' )
showHide( c, 'hide' );
else
showHide( c );
});
});
</script>
<?php
endif;
}
代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试并可以工作.
Code goes in function.php file of your active child theme (or active theme). tested and works.
这篇关于显示基于所选付款方式的隐藏自定义Woocommerce结帐字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!