Each field of the default Woocommerce form checkout has this markup:
<p class="form-row form-row-first validate-required" id="billing_first_name_field" data-priority="10">
<label for="billing_first_name" class="">Name
<abbr class="required" title="required">*</abbr>
</label>
<span class="woocommerce-input-wrapper">
<input type="text" class="input-text " name="billing_first_name" id="billing_first_name" placeholder="" value="" autocomplete="given-name">
</span>
</p>
Now, I want:
- Wrap each field in a wrapper like
<div class="single-field-wrapper"><p [...] </div>
- Wrap the first two fields, ‘billing_first_name’ and ‘billing_last_name’ in a wrapper like
<div class="first-and-second-field-wrapper"> [...] </div>
To achieve this, I’ve tried to use the filter hooks made available by function woocommerce_form_field( $key, $args, $value = null )
called in form-billing.php
template in this way:
<?php
$fields = $checkout->get_checkout_fields( 'billing' );
foreach ( $fields as $key => $field ) {
if ( isset( $field['country_field'], $fields[ $field['country_field'] ] ) ) {
$field['country'] = $checkout->get_value( $field['country_field'] );
}
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
}
?>
So in functions.php
I wrote:
function change_woocommerce_field_markup($field, $key, $args, $value){
$field = '<div class="single-field-wrapper">'.$field.'</div>';
if($key === 'billing_first_name') {
$field = '<div class="first-and-second-field-wrapper">'.$field;
}
else if ($key === 'billing_last_name') {
$field = $field.'</div>';
}
else {
$field = $field;
}
return $field;
}
add_filter("woocommerce_form_field","change_woocommerce_field_markup", 10, 4);
Unexpectedly, I get this markup:
<div class="first-and-second-field-wrapper">
<div class="single-field-wrapper">
<div class="single-field-wrapper">
[here there are all the fields, one under the other ]
</div>
<div class="single-field-wrapper"></div>
<div class="single-field-wrapper"></div>
<div class="single-field-wrapper"></div>
<div class="single-field-wrapper"></div>
<div class="single-field-wrapper"></div>
<div class="single-field-wrapper"></div>
<div class="single-field-wrapper"></div>
<div class="single-field-wrapper"></div>
</div>
Anyone can explain why this unexpected behavior?