woocommerce_form_field and hidden fields

2019-02-25 12:19发布

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.

5条回答
闹够了就滚
2楼-- · 2019-02-25 12:47

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...

add_filter('woocommerce_form_field_hidden', 'my_form_field_hidden', 4 , 15);

Now you have to set the paramteres in the filter function.

if ( ! function_exists('my_form_field_hidden') ) {

    function hp_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>' . $after;

        return $field;
    }
}

I hope it helps

查看更多
劫难
3楼-- · 2019-02-25 12:56

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.

查看更多
乱世女痞
4楼-- · 2019-02-25 12:57

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:

add_action('woocommerce_before_checkout_billing_form', array(&$this, 'custom_before_checkout_billing_form') );

Then in your own function do something like this:

function custom_before_checkout_billing_form($checkout) {

    echo '<input type="hidden" class="input-hidden" name="test" id="test" placeholder="test" value="test" />';
}
查看更多
SAY GOODBYE
5楼-- · 2019-02-25 13:04

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:

add_action( 'woocommerce_checkout_update_order_meta', 'your_hidden_data' );

function your_hidden_data( $order_id ) {
    /*
      Put your normal field saves here
    */

    //Your hidden fields
    update_post_meta( $order_id, 'YOUR DESIRED KEY NAME', 'YOUR DESIRED VALUE' );
}

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.

查看更多
走好不送
6楼-- · 2019-02-25 13:04

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:

/**
 * Add the hidden referral info field to the checkout
 */
add_action( 'woocommerce_checkout_update_order_meta', 'your_hidden_data' );

function your_hidden_data( $order_id ) {
/*
  Put your normal field saves here if needed
*/
$cookie_name1 = $_COOKIE['ref_src']; //Get my Cookie and Assign it
//Your hidden fields
update_post_meta( $order_id, 'Referral_Source', $cookie_name1 );
}
查看更多
登录 后发表回答