Show or hide checkout fields based on shipping method in Woocommerce 3

前端 未结 1 867
孤城傲影
孤城傲影 2020-12-01 17:42

I\'m trying to hide checkout fields based on shipping method.

function premove_billing_checkout_fields($fields) {
    global $woocommerce;
    $chosen_method         


        
1条回答
  •  Happy的楠姐
    2020-12-01 18:16

    Update 4 (made on June 2018)
    - Added a delay on initialization specifically for Woocommerce 3.4.x.
    - Solved a bug when shipping address checkbox is enabled.
    - Enhanced lighter and more effective jQuery code.
    - Added conditional fields validation

    You don't need any Ajax to achieve this. The first function will make all necessary checkout fields not "required", as this is necessary to be able to conditionally show / hide checkout fields. The second function (mostly jQuery), will show / hide your desired fields depending on the chosen shipping method.

    // 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 = '*';
        ?>
        
        session->get( 'chosen_shipping_methods' )[0];
        $billing                = ' ' . __('Billing', 'woocommerce') . ' ';
        $shipping               = ' ' . __('Shipping', 'woocommerce') . ' ';
        $country                = __('country.', 'woocommerce');
        $address1               = __('address.', 'woocommerce');
        $postcode               = __('postcode.', 'woocommerce');
        $state                  = __('state.', 'woocommerce');
        $end_text               = ' '. __('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' );
            }
        }
    }
    

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

    This is tested and works on WooCommerce 3+

    0 讨论(0)
提交回复
热议问题