mardi 28 novembre 2017

Setting up checkboxes as custom user meta in user admin - WooCommerce

I set up a simple checkbox field in the user account admin interface. Here is how I am displaying/saving it:

function show_free_ground_field( $user ) { 
?>

    <h3>Free Ground Shipping</h3>

    <table class="form-table">

        <tr>
            <th>Free ground for order > $1000</th>

            <td>
                <?php
                woocommerce_form_field( 'freeGround', array(
                    'type'      => 'checkbox',
                    'class'     => array('input-checkbox'),
                    'label'     => __('Yes'),
                ), '' );


                ?>

            </td>
        </tr>

    </table>
<?php 
}
add_action( 'show_user_profile', 'show_free_ground_field' );
add_action( 'edit_user_profile', 'show_free_ground_field' );

function save_free_ground_field( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) ){
        return false;
    }
    if ( ! empty( $_POST['freeGround'] ) ){
        update_usermeta( $user_id, 'freeGround', $_POST['freeGround'] );
    }
}
add_action( 'personal_options_update', 'save_free_ground_field' );
add_action( 'edit_user_profile_update', 'save_free_ground_field' );

It displays fine, but if I check it off and re-visit the same user after saving the checkbox is unchecked. How do I fix that?




Aucun commentaire:

Enregistrer un commentaire