Your Location is: Home > Php

Woocommerce, Checkout, Order -> Advanced Custom Field: set value from HTML selector

From: Luxembourg View: 1334 Schnaps 

Question

How do I set the value of a custom field on an order, upon checkout submit, to a selected value in an HTML component?

I've implemented a custom HTML component, inside the checkout form, whose selected value I want to insert in a custom field on my orders. But I can't seem to figure out how to hook these things up. I've arrived at somthing like:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
     $fields['order']['special_delivery'] = 'The value from HTML';
     return $fields;
}

But I'm not even sure that's the right way to go.

Any ideas?

Updated code:

function my_custom_field( $checkout ) {
            echo '<div id="my_custom_field">';
                woocommerce_form_field( 'my_custom_field', array(
                    'type'          => 'select',
                    'class'         => array('my_custom_field form-row-wide'),
                    'label'         => __('my_custom_field'),
                    'required'    => true,
                    'options'     => $field['choices']
                ),
                    $checkout->get_value( 'my_custom_field' )
                );
            echo '</div>';
        }
        add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_field' );
        
function populate_my_custom_field( $order_id ) {
            if ( ! empty( $_POST['my_custom_field'] ) ) {
                update_field( 'field_5ea8aef078912', sanitize_text_field( $_POST['my_custom_field'], $order_id ));
            }
        }
        add_action( 'woocommerce_checkout_update_order_meta', 'populate_my_custom_field' );

Best answer

Using woocommerce_after_checkout_billing_form allows you to add custom fields to your checkout form. You also need to save the field in the order meta using woocommerce_checkout_update_order_meta.

In the following code there is a custom text field used for saving VAT ID. Feel free to get rid of the "mrank" prefixes. Also rename the field as you need it.

It will show you how to add a field and to save in order meta.

/**
* VAT Number in WooCommerce Checkout
*/
function mrank_vat_field( $checkout ) {

      echo '<div id="mrank_vat_field">';
   
      woocommerce_form_field( 'vat_number', array(
            'type'          => 'text',
            'class'         => array( 'vat-number-field form-row-wide') ,
            'label'         => __( 'VAT-ID' ),
            'placeholder'   => __( 'Enter number' ),
            'description' => __( 'Please enter your VAT-ID' ),
            'required'    => true,
      ), $checkout->get_value( 'vat_number' ));
    
    echo '</div>';
}
add_action( 'woocommerce_after_checkout_billing_form', 'mrank_vat_field' );


/**
* Save VAT Number in the order meta
*/
function mrank_checkout_vat_number_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['vat_number'] ) ) {
        update_post_meta( $order_id, '_vat_number', sanitize_text_field( $_POST['vat_number'] ) );
    }
}
add_action( 'woocommerce_checkout_update_order_meta', 'mrank_checkout_vat_number_update_order_meta' );