Search code examples
phpwordpresswoocommercecheckoutcustom-fields

Custom hidden field on WooCommerce checkout not saving to user meta issue


I cannot figure out what i am missing here.

I am creating a hidden field on the checkout page, that contains a value after a customer's choice.

This part is working, as i can see in the inspector on the checkout page.

The hidden field should be saved to the logged-in user, as i need it on another place in the website.

I have the following:

//This part is working!!
add_action( 'woocommerce_after_checkout_billing_form', function() {
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();
    foreach($items as $item => $values) {
        if( isset($values['programmakeuze']) ){
        echo '<input type="hidden" name="programchoice" id="programchoice" class="input-hidden" value="'.$values['programmakeuze'].'">';
        }
    }
});
//Save hidden field to user
function elearning_checkout_update_user_meta( $customer_id, $posted ) {
    if (!empty($_POST['programchoice'])) {
        $program = intval($_POST['programchoice'] );
        update_user_meta( $customer_id, 'programchoice', $program);
    }
}
add_action( 'woocommerce_checkout_update_user_meta', 'elearning_checkout_update_user_meta', 10, 2 );


function testing(){
 
    $id = get_current_user_id();
    $value = get_user_meta($id,'programchoice',true);
    if ( !empty($value)) {
        var_dump ($value);
        }
    }
add_action('wp_head','testing');

The $value returns nothing. What am i missing here?


Solution

  • I've partly rewritten your code. Including the use of woocommerce_checkout_update_customer action hook.

    Also note the use of break in the for loop, as this is about a specific ID, and therefore about 1 unique field

    However, I wouldn't use the wp_head action hook for debugging. See How to debug in WooCommerce instead.

    But this should suffice, to answer your question:

    // Display a custom hidden field after checkout billing form
    function action_woocommerce_after_checkout_billing_form( $checkout ) {
        // Loop through cart items
        foreach( WC()->cart->get_cart() as $cart_item ) {
            if ( isset( $cart_item['programmakeuze'] ) ) {
                echo '<input type="hidden" name="programchoice" id="programchoice" class="input-hidden" value="' . $cart_item['programmakeuze'] . '">';
                break;
            }
        }
    }
    add_action( 'woocommerce_after_checkout_billing_form', 'action_woocommerce_after_checkout_billing_form', 10, 1 );
    
    // Save/update user data from custom field value
    function action_woocommerce_checkout_update_customer( $customer, $data ) {  
        // Isset
        if ( isset( $_POST['programchoice'] ) ) {
            $customer->update_meta_data( '_programchoice', sanitize_text_field( $_POST['programchoice'] ) );
        }
    }
    add_action( 'woocommerce_checkout_update_customer', 'action_woocommerce_checkout_update_customer', 10, 2 );
    
    // Debugging purposes
    function action_wp_head(){
        // Get user id
        $user_id = get_current_user_id();
        
        // Get user meta
        $value = get_user_meta( $user_id, '_programchoice', true );
    
        // NOT empty
        if ( ! empty( $value ) ) {
            var_dump ( $value );
        }
    }
    add_action( 'wp_head', 'action_wp_head' );