phpsessionwoocommercecustom-fieldsusermetadata

How to fill custom checkout field with previously entered value, like default WooCommerce checkout fields?


I have added a custom field using the below code:

add_action( 'woocommerce_before_order_notes', 'bbloomer_add_custom_checkout_field' );
function bbloomer_add_custom_checkout_field( $checkout ) { 
   $current_user = wp_get_current_user();
   $saved_gst_no = $current_user->gst_no;
   
   woocommerce_form_field( 'gst_no', array(        
       'type'        => 'text',        
       'class'       => array( 'form-row-wide' ),        
       'label'       => 'GST Number',        
       'placeholder' => 'GST Number',        
       'required'    => true
       //'default'   => $saved_gst_no,        
   ), $checkout->get_value( 'gst_no' ) ); 
}

On entering any value in GST Number field (custom checkout field), then going to payment screen by clicking "Place order" button and returning to checkout page without completing the transaction, all default woocommerce fields like billing phone, email etc are auto filled from the session.

However, the custom field added via above code is always blank. How to get the previously entered value auto-filled in the custom field for guest users, similar to the way default woocommerce fields are auto-filled?


Solution

  • Updated (replaced wrong WC_Session method set() with get() on the first function)

    This will work for guest users too. Replace your code with:

    // Display checkout custom field
    add_action( 'woocommerce_before_order_notes', 'add_custom_checkout_field' );
    function add_custom_checkout_field( $checkout ) { 
        $key_field = 'gst_no';
           
        woocommerce_form_field( $key_field, array(        
            'type'        => 'text',        
            'class'       => array( 'form-row-wide' ),        
            'label'       => __('GST Number'),        
            'placeholder' => __('GST Number'),        
            'required'    => true
            //'default'   => $saved_gst_no,        
        ), $checkout->get_value($key_field) ? $checkout->get_value($key_field) : WC()->session->get($key_field) ); 
    }
    
    // Save checkout custom field value in a WC_Session variable 
    add_action( 'woocommerce_checkout_create_order', 'action_checkout_create_order', 10, 2 );
    function action_checkout_create_order( $order, $data  ) {
        $key_field = 'gst_no';
        
        if( isset($_POST[$key_field]) ) {
            WC()->session->set($key_field, sanitize_text_field($_POST[$key_field]));
        }
    }
    
    // Save checkout custom field value as user meta data
    add_action( 'woocommerce_checkout_update_customer', 'action_checkout_update_customer', 10, 2 );
    function action_checkout_update_customer( $customer, $data  ) {
        $key_field = $key_field;
        
        if( isset($_POST['gst_no']) ) {
            $customer->update_meta_data($key_field, sanitize_text_field($_POST[$key_field]));
        }
    }
    

    Note: Uses a WC_Session variable to store the submitted value for guests, allowing to display it on checkout when returning without completing the transaction.