0

I added an extra registration field:

function wooc_extra_register_fields() {
    ?><p class="form-row form-row-first">
        <label for="billing_cpf"><?php _e( 'CPF', 'woocommerce' ); ?> <span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_cpf" id="billing_cpf" value="<?php 
            if ( ! empty( $_POST['billing_cpf'] ) ) esc_attr_e( $_POST['billing_cpf'] ); ?>" />
    </p><?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );

function wooc_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST['billing_cpf'] ) ) {
        update_user_meta( $customer_id, 'billing_cpf', sanitize_text_field( $_POST['billing_cpf'] ) );
    }
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

I added a mask and some functions to validate the information. This information is saved and shown on the WordPress dashboard with this code:

function theme_add_user_cpf_column( $columns ) {
    $columns['billing_cpf'] = __( 'CPF', 'theme' );
    return $columns;
}
add_filter( 'manage_users_columns', 'theme_add_user_cpf_column' );

function theme_show_user_cpf_data( $value, $column_name, $user_id ) {
    if( 'billing_cpf' == $column_name ) {
        return get_user_meta( $user_id, 'billing_cpf', true );
    }
}
add_action( 'manage_users_custom_column', 'theme_show_user_cpf_data', 10, 3 );

Everything works fine, but I need to check if meta billing_cpf already exists before saving (unique information per user). How can I do that?

2 Answers 2

0
$havemeta = get_user_meta($user_id, 'billing_cpf', false);
if ($havemeta)
     // do something
} else {
    // do something
}

Try This

0

You can use this

$prev_value = get_post_meta($customer_id, "billing_cpf", true);

    if ( isset( $_POST['billing_cpf'] ) && trim($_POST['billing_cpf']) != "" ) {

          if($prev_value  != "") {

                // do something

           } else {

                // do another thing
    
           }
    }
1
  • thanks, but not working. It brings always "else" condition. Sep 9, 2015 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.