Skip to content
Advertisement

How to save from WooCommerce checkout a custom checkbox field state?

I have a problem with the update_post_meta function. I have a user submitted value, which I pass via $_POST and then saving to post meta.

All is working fine, but when the value is ‘0’ the post meta is not updated.

This is My code:

// Add custom checkout field: woocommerce_review_order_before_submit
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field_ritiro_sede' );
function my_custom_checkout_field_ritiro_sede() {
    echo '<div class="cw_custom_class"><h3>'.__('Ritiro presso sede CER S.r.l. &nbsp').'</h3>';
    echo '<div id="my_custom_checkout_field">';
    woocommerce_form_field( 'ritiro_sede', array(
        'type'      => 'checkbox',
        'class'     => array('input-checkbox'),
        'label'     => __('SI'),
    ),  WC()->checkout->get_value( 'ritiro_sede' ) );
    echo '</div>';
}

// Save the custom checkout field in the order meta, when checkbox has been checked
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta_ritiro_sede', 10, 1 );
function custom_checkout_field_update_order_meta_ritiro_sede( $order_id ) {

    if ( ! empty( $_POST['ritiro_sede'] ) )
        update_post_meta( $order_id, 'ritiro_sede', $_POST['ritiro_sede'] );
    if ( isset( $_POST['ritiro_sede'] ) )
        update_post_meta( $order_id, 'ritiro_sede', $_POST['0'] );
    
}

Does anyone have any idea what might be wrong?

Advertisement

Answer

Since WooCommerce 3, here below is the best way to save your custom checkout checkbox field value as order meta data (including when the checkbox is unchecked):

// Save the custom checkout checkbox field as the order meta
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta', 10, 2 );
function custom_checkout_field_update_order_meta( $order, $data ) {
    $value = isset($_POST['ritiro_sede']) ? '1' : '0'; // Set the correct values
    
    $order->update_meta_data( 'ritiro_sede', $value );
}

Now as user meta data is used by WC_Checkout get_value() method in your first function on:

WC()->checkout->get_value( 'ritiro_sede' )

So if you want the submitted value to be displayed on checkout page for the next purchase, you will need to save that custom checkout field also as user meta data using instead the following:

// Save the custom checkout checkbox field as the order meta and user meta
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta', 10, 2 );
function custom_checkout_field_update_order_meta( $order, $data ) {
    $value = isset($_POST['ritiro_sede']) ? '1' : '0'; // Set the correct values
    
    // Save as custom order meta data
    $order->update_meta_data( 'ritiro_sede', $value );

    // Save as custom user meta data
    if ( get_current_user_id() > 0 ) {
        update_user_meta( get_current_user_id(), 'ritiro_sede', $value );
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement