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.
You have to pass the parameters when you add your filter.. Something like
The third parameter in add_filter function is the numbers of parameter that the filter receive.
The last parameter is the priority...
Now you have to set the paramteres in the filter function.
I hope it helps
Actually. The last paramatert of the add_filter function is the number of parameters to the function.
The third one is the priority.
This worked for me.
I'm not sure exactly how you are adding the other non-hidden custom fields, but you can just echo html.
i.e.
Add a hook:
Then in your own function do something like this:
I know it's been awhile since you asked this question, but I found something that worked for me. I was able to get around a hidden field by posting certain information to the post meta.
This is what I did:
Above where I have "YOUR DESIRED VALUE, I placed a function that returned a number that I needed saved to the order.
Hopefully this is not too specific to my own application.
If you can pull the info you need and put it into a variable you can totally bypass the need to put the info in the form. Just add the info directly to update_post_meta.
I needed to add a value stored in a COOKIE and originally set out to add it as hidden field on the form but end up doing this instead: