Show or Hide two checkout fields in Woocommerce

爷,独闯天下 提交于 2019-11-30 09:37:32

问题


In Woocommerce checkout form im trying to hide billing_city and billing_address_1 fields first and after I fill the fields postcode and housenumber the other fields (address_1 and city) has to show.

This is the script, but I does not work well and I dont know what I do wrong:

?>
<script type="text/javascript">
    jQuery('input#billing_housenumber').change(function(){

        if (this.checked) {
            jQuery('#billing_city').fadeIn();
            jQuery('#billing_city input').val('');           
        } else {
            jQuery('#billing_city').fadeOut();
        }

    });
</script>
<?php

Any help is appreciated.


回答1:


Update 2

There is some errors, mistakes and missing things in your code. Also you should hooked it in a function (or register it as an external file).

I have added at the end a function that add the billing "housenumber" field in checkout (just for testing purpose)

In the code bellow I am using some CSS inline styles to hide the billing city and address_1 fields and to show them when they have an additional selector class "on".

Here is a hooked function that will enable this code in checkout only:

add_action( 'wp_footer', 'custom_checkout_script' );
function custom_checkout_script() {
    // Only checkout page
    if( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return;

    // CSS Styles (Hidding fields)
    ?>
    <style>
        #billing_city_field, #billing_address_1_field { display:none !important;}
        #billing_city_field.on ,#billing_address_1_field.on { display:block!important;}
    </style>
    <?php 
    // Jquery script
    ?>
    <script type="text/javascript">
    jQuery( function($){
        var a = 'input#billing_housenumber',
            b = 'input#billing_postcode',
            c = '#billing_city_field,#billing_address_1_field';

        // On start
        if( $(a).val() != '' && $(b).val() != '' && ! $(c).hasClass('on') ){
            $(c).css('display','none !important').addClass('on').show();
            console.log('start');
        }

        // On change: If housenumber and postcode fields are filled, we display other hidden fields
        $(a+','+b).on( 'change', function(){
            if ( $(a).val() != '' && $(b).val() != '' && ! $(c).hasClass('on') ) {
                $(c).css('display','none !important').addClass('on').show( 500 );
                console.log('change - in');
            } else if( ( $(a).val() == '' || $(b).val() == '' ) && $(c).hasClass('on') ) {
                $(c).hide( 200, function(){
                    $(c).removeClass('on').css('display','block')
                });
                console.log('change - out');
            }
        });
    });
    </script>
    <?php
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


This code has been tested with this temporary additional code, that generates the checkout "housenumber" text field (just for testing purpose):

// Add custom checkout field
add_action( 'woocommerce_billing_fields', 'my_custom_checkout_field' );
function my_custom_checkout_field( $fields ) {

     $fields['billing_housenumber'] = array(
        'class'     => array('form-row-wide'),
        'label'     => __('Housenumber ?'),
        'required'  => false,
        'clear'     => true
     );

     return $fields;
}

Code goes in function.php file of your active child theme (or active theme).


Related: Show or hide html element on chosen shipping method change in Woocommerce




回答2:


There is one step left. I don't see the housenumber (after a customer purchase a product). So you don't see the housenumber in the mail or in invoice.

This is in the e-mail but I don't see more detail for address:

/**
  * @hooked WC_Emails::customer_details() Shows customer details
  * @hooked WC_Emails::email_address() Shows email address
  */
 do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );


来源:https://stackoverflow.com/questions/51847083/show-or-hide-two-checkout-fields-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!