隐藏特定的woocommerce帐单字段

I have an url to woocommerce checkout, with variables to put into the billing fields automatically, so they should not be shown at the checkout page. How can we do so?

Adding type="hidden" to the input fields?

Thanks.

We should minimize "user involvement" as much as we can (removing type=hidden is a very easy thing to do), so if you have data that should be automatically added, I would advise you to disable those fields completely and insert data via WooCommerce filters.

1. Remove fields you want to hide

First filter that you need is woocommerce_billing_fields. With that filter, you can remove or add fields. It accepts one argument, and that's array of billing fields. For example:

/**
 * Used to remove WooCommerce billing fields
 * 
 * @param array $fields
 * 
 * @return array Modified billing fields
 */
function so_remove_billing_fields( $fields ){
    // FYI: These are all default WooCommerce billing fields
    // (remove those which need to stay)
    $fields_to_remove = array(
        'billing_first_name',
        'billing_last_name',
        'billing_company',
        'billing_address_1',
        'billing_address_2',
        'billing_city',
        'billing_postcode',
        'billing_country',
        'billing_state',
        'billing_email',
        'billing_phone',
    );

    foreach( $fields_to_remove as $field ){
        unset( $fields[$field] );
    }

    return $fields;
}
add_filter( 'woocommerce_billing_fields', 'so_remove_billing_fields' );

2. Add data that should be saved

We should now save the custom data to the post (order) meta, and for that, we will use woocommerce_checkout_update_order_meta filter. It has one argument, and that is integer order ID. For example:

/**
 * Save custom data to the database
 * 
 * @param int $order_id
 *
 * @return void
 */
function so_add_billing_fields_data( $order_id ){
    $data_to_add = array(
        'field_name' => apply_filters( 'so_custom_field_name_data', null ),
    );

    foreach( $data_to_add as $field_name => $data ){
        if ( ! empty( $data ) )
            update_post_meta( $order_id, $field_name, sanitize_text_field( $data ) );
    }
}
add_filter( 'woocommerce_checkout_update_order_meta', 'so_add_billing_fields_data' );

Maybe sanitize_text_field() was not necessary here, but better safe than sorry. I don't know the source of your data. ;)

By the way, I opted for apply_filters in this example to add data to the array, but if you can also gather data in this function and put it into a variable.


See more: WooCommerce: Customizing checkout fields | WordPress: Filter API Reference | Validating Sanitizing and Escaping User Data

Yes, they are telling you, there's no type hidden on billing fields.
But there's room for it if you'll create one.

something like this would do.

1. First, let's change the field type to hidden depending on what is passed on the url.

function woocommerce_billing_fields( $fields ){
    // let's not do anything if not on checkout page.
    if ( !is_checkout() ) return $fields; 

    // these are expected fields. 
    // please remove those that are not needed.
    $url_param_fields = array(
        'first_name',
        'last_name',
        'company',
        'address_1',
        'address_2',
        'city',
        'postcode',
        'country',
        'state',
        'email',
        'phone',
    );

    foreach( $url_param_fields as $param ){
        $billing_key = 'billing_' . $param;
        if ( isset( $_GET[$param] ) && array_key_exists( $billing_key, $fields) ) {
            $fields[$billing_key]['type'] = 'hidden'; // let's change the type of this to hidden.
            $fields[$billing_key]['default'] = $_GET[$param]; // you need to sanitized $_GET[$param] here.
        }
    }

    return $fields;
}
add_filter( 'woocommerce_billing_fields', 'woocommerce_billing_fields' );

2. Of course there's no type hidden as for the default WooCommerce.

But below code will create it.

function woocommerce_form_field_hidden( $field, $key, $args ){

    $field = '<input type="hidden" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="' . esc_attr( $args['default'] ) . '" />';

    return $field;
}
add_filter( 'woocommerce_form_field_hidden', 'woocommerce_form_field_hidden', 10, 3 );

The expected url would then be something like http://reigelgallarde.me/checkout/?company=reigelgallarde.me. In this case the billing company field will be change to something like

 <input type="hidden" name="billing_company" id="billing_company" value="reigelgallarde.me">