woocommerce_form_field and hidden fields

Sebastien picture Sebastien · Aug 13, 2012 · Viewed 11.3k times · Source

I'm trying to add custom fields to the WooCommerce checkout and there seems to be no output for hidden fields.

In woocommerce-template.php, hidden fields fall into this switch case :

default :
    $field = apply_filters( 'woocommerce_form_field_' . $args['type'], '', $key, $args, $value  );
    break;
}

How would I go about adding a woocommerce_form_field_hidden action which outputs a hidden field. I've tried multiple things which don't work. Ultimately, I'm not able to figure out how to pass the function parameters.

add_action('woocommerce_form_field_hidden', 'my_form_field_hidden');
if ( ! function_exists('my_form_field_hidden') ) {
    function hp_form_field_hidden() {

        $field = '<p class="form-row ' . implode( ' ', $args['class'] ) .'" id="' . $key . '_field">
            <input type="hidden" class="input-hidden" name="' . $key . '" id="' . $key . '" placeholder="' . $args['placeholder'] . '" value="'. $value.'" />
            </p>' . $after;

        return $field;
    }
}

All the help is appreciated.

Answer

tormorten picture tormorten · Sep 2, 2014

Actually. The last paramatert of the add_filter function is the number of parameters to the function.

The third one is the priority.

add_filter('woocommerce_form_field_hidden', 'wcds_form_field_hidden', 999, 4);

function wcds_form_field_hidden($no_parameter, $key, $args, $value) {

    $field = '<p class="form-row ' . implode( ' ', $args['class'] ) .'" id="' . $key . '_field">
        <input type="hidden" class="input-hidden" name="' . $key . '" id="' . $key . '" placeholder="' . $args['placeholder'] . '" value="'. $value.'" />
        </p>';

    return $field;
}

This worked for me.