Javascript 根据Woocommerce 3中的配送方式显示或隐藏签出字段
我正试图根据装运方法隐藏签出字段Javascript 根据Woocommerce 3中的配送方式显示或隐藏签出字段,javascript,php,jquery,wordpress,woocommerce,Javascript,Php,Jquery,Wordpress,Woocommerce,我正试图根据装运方法隐藏签出字段 function premove_billing_checkout_fields($fields) { global $woocommerce; $chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); $chosen_shipping = $chosen_methods[0]; if( $chosen_shipping === 'local
function premove_billing_checkout_fields($fields) {
global $woocommerce;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if( $chosen_shipping === 'local_pickup:20' ) {
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_state']);
unset($fields['billing']['billing_country']);
}
if( $chosen_shipping === 'wc_custom_shipping_pickpoint' ) {
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_state']);
}
return $fields;
}
add_filter('woocommerce_checkout_fields',
'premove_billing_checkout_fields', 990 );
// Conditional Show hide checkout fields based on chosen shipping methods
add_action( 'wp_footer', 'custom_checkout_field_script' );
function custom_checkout_field_script() {
// HERE your shipping methods rate IDs
$local_pickup = 'local_pickup:20';
$pickpoint = 'wc_custom_shipping_pickpoint';
$required_text = esc_attr__( 'required', 'woocommerce' );
$required_html = '<abbr class="required" title="' . $required_text . '">*</abbr>';
?>
<script>
jQuery(function($){
var ism = 'input[name^="shipping_method"]', ismc = ism+':checked',
csa = 'input#ship-to-different-address-checkbox',
rq = '-required', vr = 'validate'+rq, w = 'woocommerce', wv = w+'-validated',
iv = '-invalid', fi = '-field', wir = w+iv+' '+w+iv+rq+fi,
b = '#billing_', s = '#shipping_', f = '_field',
a1 = 'country', a2 = 'address_1', a3 = 'address_2', a4 = 'postcode', a5 = 'state',
b1 = b+a1+f, b2 = b+a2+f, b3 = b+a3+f, b4 = b+a4+f, b5 = b+a5+f,
s1 = s+a1+f, s2 = s+a2+f, s3 = s+a3+f, s4 = s+a4+f, s5 = s+a5+f,
pickPoint = '<?php echo $pickpoint; ?>', localPickup = '<?php echo $local_pickup; ?>';
// Utility function to shows or hide checkout fields
function showHide( action='show', selector='' ){
if( action == 'show' )
$(selector).show(function(){
$(this).addClass(vr);
$(this).removeClass(wv);
$(this).removeClass(wir);
if( $(selector+' > label > abbr').html() == undefined )
$(selector+' label').append('<?php echo $required_html; ?>');
});
else
$(selector).hide(function(){
$(this).removeClass(vr);
$(this).removeClass(wv);
$(this).removeClass(wir);
if( $(selector+' > label > abbr').html() != undefined )
$(selector+' label > .required').remove();
});
}
// Initializing at start after checkout init (Based on the chosen shipping method)
setTimeout(function(){
if( $(ismc).val() == pickPoint ) // Chosen "Pick point" (Hiding "Delivery")
{
showHide('show',b1 ); // Country
showHide('hide',b2 ); // Address 1
showHide('hide',b3 ); // Address 2
showHide('hide',b4 ); // Postcode
showHide('hide',b5 ); // State
}
else if( $(ismc).val() == localPickup ) // Choosen "Local pickup" (Hidding "Take away")
{
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('hide',b4);
showHide('hide',b5);
}
else
{
showHide('show',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b5);
}
}, 100);
// When shipping method is changed (Live event)
$( 'form.checkout' ).on( 'change', ism, function() {
if( $(ismc).val() == pickPoint )
{
showHide('show',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b5);
if( $(csa).prop('checked') ) {
showHide('show',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
}
}
else if( $(ismc).val() == localPickup )
{
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('hide',b4);
showHide('hide',b5);
if( $(csa).prop('checked') ) {
showHide('hide',s1);
showHide('show',s2);
showHide('show',s3);
showHide('hide',s4);
showHide('hide',s5);
}
}
else
{
showHide('show',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b5);
if( $(csa).prop('checked') ) {
showHide('show',s1);
showHide('show',s2);
showHide('show',s3);
showHide('show',s4);
showHide('show',s5);
}
}
});
// When "shipping to different address" is changed (Live event)
$(csa).click( function() {
if( $(ismc).val() == pickPoint && $(this).prop('checked') )
{
showHide('show',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b4);
showHide('show',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
}
else if( $(ismc).val() == localPickup && $(this).prop('checked') )
{
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('hide',b4);
showHide('hide',b4);
showHide('hide',s1);
showHide('show',s2);
showHide('show',s3);
showHide('hide',s4);
showHide('hide',s5);
}
else
{
showHide('show',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b4);
showHide('show',s1);
showHide('show',s2);
showHide('show',s3);
showHide('show',s4);
showHide('show',s5);
}
});
});
</script>
<?php
}
// Checkout conditional fields validation
add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
function wps_select_checkout_field_process() {
// HERE your shipping methods rate IDs
$local_pickup = 'local_pickup:20';
$pickpoint = 'wc_custom_shipping_pickpoint';
$chosen_shipping_method = WC()->session->get( 'chosen_shipping_methods' )[0];
$billing = '<strong> ' . __('Billing', 'woocommerce') . ' ';
$shipping = '<strong> ' . __('Shipping', 'woocommerce') . ' ';
$country = __('country.', 'woocommerce');
$address1 = __('address.', 'woocommerce');
$postcode = __('postcode.', 'woocommerce');
$state = __('state.', 'woocommerce');
$end_text = '</strong> '. __('is a required field.', 'woocommerce');
if( $chosen_shipping_method == $local_pickup ) {
if( empty($_POST['billing_address_1']) )
wc_add_notice( $billing . $address1 . $end_text, 'error' );
if( $_POST['ship_to_different_address'] ){
if( empty($_POST['shipping_address_1']) )
wc_add_notice( $shipping . $address1 . $end_text, 'error' );
}
}
elseif( $chosen_shipping_method == $pickpoint ) {
if( empty($_POST['billing_country']) )
wc_add_notice( $billing . $country . $end_text, 'error' );
if( $_POST['ship_to_different_address'] ){
if( empty($_POST['shipping_country']) )
wc_add_notice( $shipping . $country . $end_text, 'error' );
}
}
else {
if( empty($_POST['billing_country']) )
wc_add_notice( $billing . $country . $end_text, 'error' );
if( empty($_POST['billing_address_1']) )
wc_add_notice( $billing . $address1 . $end_text, 'error' );
if( empty($_POST['billing_postcode']) )
wc_add_notice( $billing . $postcode . $end_text, 'error' );
if( empty($_POST['billing_state']) )
wc_add_notice( $billing . $state . $end_text, 'error' );
if( $_POST['ship_to_different_address'] ){
if( empty($_POST['shipping_country']) )
wc_add_notice( $shipping . $country . $end_text, 'error' );
if( empty($_POST['shipping_address_1']) )
wc_add_notice( $shipping . $address1 . $end_text, 'error' );
if( empty($_POST['shipping_postcode']) )
wc_add_notice( $shipping . $postcode . $end_text, 'error' );
if( empty($_POST['shipping_state']) )
wc_add_notice( $shipping . $state . $end_text, 'error' );
}
}
}
这段代码正在工作,但要隐藏字段,我需要刷新页面。
如何使用Ajax隐藏字段
更新4(于2018年6月制作)-专门为Woocommerce 3.4.x添加了初始化延迟
-解决了启用“配送地址”复选框时出现的错误
-增强的更轻、更有效的jQuery代码
-添加了条件字段验证 您不需要任何Ajax来实现这一点。第一个函数将使所有必要的签出字段都不是“必需的”,因为这是能够有条件地显示/隐藏签出字段所必需的。第二个函数(主要是jQuery)将根据所选的发送方式显示/隐藏所需的字段
function premove_billing_checkout_fields($fields) {
global $woocommerce;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if( $chosen_shipping === 'local_pickup:20' ) {
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_state']);
unset($fields['billing']['billing_country']);
}
if( $chosen_shipping === 'wc_custom_shipping_pickpoint' ) {
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_state']);
}
return $fields;
}
add_filter('woocommerce_checkout_fields',
'premove_billing_checkout_fields', 990 );
// Conditional Show hide checkout fields based on chosen shipping methods
add_action( 'wp_footer', 'custom_checkout_field_script' );
function custom_checkout_field_script() {
// HERE your shipping methods rate IDs
$local_pickup = 'local_pickup:20';
$pickpoint = 'wc_custom_shipping_pickpoint';
$required_text = esc_attr__( 'required', 'woocommerce' );
$required_html = '<abbr class="required" title="' . $required_text . '">*</abbr>';
?>
<script>
jQuery(function($){
var ism = 'input[name^="shipping_method"]', ismc = ism+':checked',
csa = 'input#ship-to-different-address-checkbox',
rq = '-required', vr = 'validate'+rq, w = 'woocommerce', wv = w+'-validated',
iv = '-invalid', fi = '-field', wir = w+iv+' '+w+iv+rq+fi,
b = '#billing_', s = '#shipping_', f = '_field',
a1 = 'country', a2 = 'address_1', a3 = 'address_2', a4 = 'postcode', a5 = 'state',
b1 = b+a1+f, b2 = b+a2+f, b3 = b+a3+f, b4 = b+a4+f, b5 = b+a5+f,
s1 = s+a1+f, s2 = s+a2+f, s3 = s+a3+f, s4 = s+a4+f, s5 = s+a5+f,
pickPoint = '<?php echo $pickpoint; ?>', localPickup = '<?php echo $local_pickup; ?>';
// Utility function to shows or hide checkout fields
function showHide( action='show', selector='' ){
if( action == 'show' )
$(selector).show(function(){
$(this).addClass(vr);
$(this).removeClass(wv);
$(this).removeClass(wir);
if( $(selector+' > label > abbr').html() == undefined )
$(selector+' label').append('<?php echo $required_html; ?>');
});
else
$(selector).hide(function(){
$(this).removeClass(vr);
$(this).removeClass(wv);
$(this).removeClass(wir);
if( $(selector+' > label > abbr').html() != undefined )
$(selector+' label > .required').remove();
});
}
// Initializing at start after checkout init (Based on the chosen shipping method)
setTimeout(function(){
if( $(ismc).val() == pickPoint ) // Chosen "Pick point" (Hiding "Delivery")
{
showHide('show',b1 ); // Country
showHide('hide',b2 ); // Address 1
showHide('hide',b3 ); // Address 2
showHide('hide',b4 ); // Postcode
showHide('hide',b5 ); // State
}
else if( $(ismc).val() == localPickup ) // Choosen "Local pickup" (Hidding "Take away")
{
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('hide',b4);
showHide('hide',b5);
}
else
{
showHide('show',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b5);
}
}, 100);
// When shipping method is changed (Live event)
$( 'form.checkout' ).on( 'change', ism, function() {
if( $(ismc).val() == pickPoint )
{
showHide('show',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b5);
if( $(csa).prop('checked') ) {
showHide('show',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
}
}
else if( $(ismc).val() == localPickup )
{
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('hide',b4);
showHide('hide',b5);
if( $(csa).prop('checked') ) {
showHide('hide',s1);
showHide('show',s2);
showHide('show',s3);
showHide('hide',s4);
showHide('hide',s5);
}
}
else
{
showHide('show',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b5);
if( $(csa).prop('checked') ) {
showHide('show',s1);
showHide('show',s2);
showHide('show',s3);
showHide('show',s4);
showHide('show',s5);
}
}
});
// When "shipping to different address" is changed (Live event)
$(csa).click( function() {
if( $(ismc).val() == pickPoint && $(this).prop('checked') )
{
showHide('show',b1);
showHide('hide',b2);
showHide('hide',b3);
showHide('hide',b4);
showHide('hide',b4);
showHide('show',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
}
else if( $(ismc).val() == localPickup && $(this).prop('checked') )
{
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
showHide('hide',b4);
showHide('hide',b4);
showHide('hide',s1);
showHide('show',s2);
showHide('show',s3);
showHide('hide',s4);
showHide('hide',s5);
}
else
{
showHide('show',b1);
showHide('show',b2);
showHide('show',b3);
showHide('show',b4);
showHide('show',b4);
showHide('show',s1);
showHide('show',s2);
showHide('show',s3);
showHide('show',s4);
showHide('show',s5);
}
});
});
</script>
<?php
}
// Checkout conditional fields validation
add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
function wps_select_checkout_field_process() {
// HERE your shipping methods rate IDs
$local_pickup = 'local_pickup:20';
$pickpoint = 'wc_custom_shipping_pickpoint';
$chosen_shipping_method = WC()->session->get( 'chosen_shipping_methods' )[0];
$billing = '<strong> ' . __('Billing', 'woocommerce') . ' ';
$shipping = '<strong> ' . __('Shipping', 'woocommerce') . ' ';
$country = __('country.', 'woocommerce');
$address1 = __('address.', 'woocommerce');
$postcode = __('postcode.', 'woocommerce');
$state = __('state.', 'woocommerce');
$end_text = '</strong> '. __('is a required field.', 'woocommerce');
if( $chosen_shipping_method == $local_pickup ) {
if( empty($_POST['billing_address_1']) )
wc_add_notice( $billing . $address1 . $end_text, 'error' );
if( $_POST['ship_to_different_address'] ){
if( empty($_POST['shipping_address_1']) )
wc_add_notice( $shipping . $address1 . $end_text, 'error' );
}
}
elseif( $chosen_shipping_method == $pickpoint ) {
if( empty($_POST['billing_country']) )
wc_add_notice( $billing . $country . $end_text, 'error' );
if( $_POST['ship_to_different_address'] ){
if( empty($_POST['shipping_country']) )
wc_add_notice( $shipping . $country . $end_text, 'error' );
}
}
else {
if( empty($_POST['billing_country']) )
wc_add_notice( $billing . $country . $end_text, 'error' );
if( empty($_POST['billing_address_1']) )
wc_add_notice( $billing . $address1 . $end_text, 'error' );
if( empty($_POST['billing_postcode']) )
wc_add_notice( $billing . $postcode . $end_text, 'error' );
if( empty($_POST['billing_state']) )
wc_add_notice( $billing . $state . $end_text, 'error' );
if( $_POST['ship_to_different_address'] ){
if( empty($_POST['shipping_country']) )
wc_add_notice( $shipping . $country . $end_text, 'error' );
if( empty($_POST['shipping_address_1']) )
wc_add_notice( $shipping . $address1 . $end_text, 'error' );
if( empty($_POST['shipping_postcode']) )
wc_add_notice( $shipping . $postcode . $end_text, 'error' );
if( empty($_POST['shipping_state']) )
wc_add_notice( $shipping . $state . $end_text, 'error' );
}
}
}
//根据所选的装运方法有条件显示隐藏签出字段
添加_操作(“wp_页脚”、“自定义_签出_字段_脚本”);
函数自定义\签出\字段\脚本(){
//这是您的运输方式和费率ID
$local_皮卡='local_皮卡:20';
$pickpoint='wc_定制_运输_pickpoint';
$required_text=esc_attr_uuu('required','woocommerce');
$required_html='*';
?>
jQuery(函数($){
var ism='input[name^=“shipping_method”]',ismc=ism+':选中',
csa=‘输入#发送到不同地址复选框’,
rq='-required',vr='validate'+rq,w='woocommerce',wv=w+'-validated',,
iv='-invalid',fi='-field',wir=w+iv+''+w+iv+rq+fi,
b='#账单",s='#发货",f='"字段",
a1=国家,a2=地址1,a3=地址2,a4=邮政编码,a5=州,
b1=b+a1+f,b2=b+a2+f,b3=b+a3+f,b4=b+a4+f,b5=b+a5+f,
s1=s+a1+f,s2=s+a2+f,s3=s+a3+f,s4=s+a4+f,s5=s+a5+f,
拾取点=“”,本地拾取=“”;
//用于显示或隐藏签出字段的实用程序函数
函数showHide(action='show',selector=''){
如果(操作=='show')
$(选择器).show(函数(){
$(此).addClass(vr);
$(此).removeClass(wv);
$(此).removeClass(wir);
if($(选择器+'>label>abbr').html()==未定义)
$(选择器+“标签”)。附加(“”);
});
其他的
$(选择器).隐藏(函数(){
$(此).removeClass(vr);
$(此).removeClass(wv);
$(此).removeClass(wir);
if($(选择器+'>label>abbr').html()!=未定义)
$(选择器+'label>.required')。删除();
});
}
//签出初始化后在开始时初始化(基于所选的装运方法)
setTimeout(函数(){
if($(ismc.val()==pickPoint)//选择“pickPoint”(隐藏“Delivery”)
{
showHide('show',b1);//国家/地区
showHide('hide',b2);//地址1
showHide('hide',b3);//地址2
showHide('hide',b4);//邮政编码
showHide('hide',b5);//状态
}
else if($(ismc.val()==本地拾取)//选择“本地拾取”(隐藏“外卖”)
{
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
显示隐藏(“隐藏”,b4);
showHide('hide',b5);
}
其他的
{
showHide('show',b1);
showHide('show',b2);
showHide('show',b3);
显示隐藏(“显示”,b4);
showHide('show',b5);
}
}, 100);
//装运方式更改时(实时事件)
$('form.checkout')。在('change',ism,function()上{
if($(ismc).val()==选取点)
{
showHide('show',b1);
showHide('hide',b2);
showHide('hide',b3);
显示隐藏(“隐藏”,b4);
showHide('hide',b5);
如果($(csa).prop('选中')){
showHide('show',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
}
}
else if($(ismc).val()==本地拾取)
{
showHide('hide',b1);
showHide('show',b2);
showHide('show',b3);
显示隐藏(“隐藏”,b4);
showHide('hide',b5);
如果($(csa).prop('选中')){
showHide('hide',s1);
showHide('show',s2);
showHide('show',s3);
showHide('hide',s4);
showHide('hide',s5);
}
}
其他的
{
showHide('show',b1);
showHide('show',b2);
showHide('show',b3);
显示隐藏(“显示”,b4);
showHide('show',b5);
如果($(csa).prop('选中')){
showHide('show',s1);
showHide('show',s2);
showHide('show',s3);
showHide('show',s4);