custom field - Check if value exists before saving

admin2025-01-07  5

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?

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?

Share Improve this question edited Sep 4, 2015 at 22:03 Gabriel 2,24810 gold badges22 silver badges24 bronze badges asked Sep 4, 2015 at 20:38 Paulo MoraesPaulo Moraes 11 bronze badge
Add a comment  | 

2 Answers 2

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

Try This

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
    
           }
    }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736253814a154.html

最新回复(0)