0
votes

I'm trying to add an extra form field to woocommerce checkout, and populate it with an existing user meta value if exists, but I can't seem to get the value attribute to render.

$user_meta_billing_licence_number traces to the error log, and it will show up in the label if I append it to the label, and to the placeholder, if I append it to the placeholder, but not to the value attribute. What am I doing wrong?

add_filter('woocommerce_billing_fields', 'custom_billing_fields', 20 );
function custom_billing_fields( $fields ) {
    // Add license field.
    $user_id = get_current_user_id();
    $user_meta_billing_licence_number = (string)get_user_meta( $user_id, 'billing_licence_number', true );
    error_log('debug: line 117: billing_licence_number = '.$user_meta_billing_licence_number);
    $fields['billing_licence_number'] = array(
        'label'     => __('License Number (for use on certificates)', 'woocommerce'),
        'required'  => false,
        'class'     => array('form-row-wide'),
        'clear'     => true,
        'placeholder' => 'Recommended format: MA12345'.$user_meta_billing_licence_number,
        'value'     => $user_meta_billing_licence_number
    );
    return $fields;
}
1

1 Answers

0
votes

So... looks like you can't pass the value in through this filter. You have to intercept it later, and override the value fetching during the printing of the fields:

<?php foreach ( $checkout->checkout_fields['billing'] as $key => $field ) : ?>

    <?php

        if($key == 'billing_licence_number') {
            $uID = get_current_user_id();
            woocommerce_form_field( $key, $field, get_user_meta($uID, 'billing_licence_number', true));
        } else {
            woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
        }

    ?>

<?php endforeach; ?>