woocommerce_form_field and hidden fields

前端 未结 5 676
[愿得一人]
[愿得一人] 2021-01-14 17:18

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 fie

相关标签:
5条回答
  • 2021-01-14 17:34

    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

    0 讨论(0)
  • 2021-01-14 17:46

    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 );
    }
    
    0 讨论(0)
  • 2021-01-14 17:54

    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.

    0 讨论(0)
  • 2021-01-14 17:56

    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" />';
    }
    
    0 讨论(0)
  • 2021-01-14 17:59

    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.

    0 讨论(0)
提交回复
热议问题