3
votes

I want to save my custom field to new user meta. My custom field = T.C. Kimlik No

The functions at the beginning of the code line are for adding the field to the membership section.

The main thing I want to do is to automatically pull my custom area, which I control at the top, when it comes to the checkout page for registered users

/** TC Kimlik No Ekleme **/

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
$fields['billing']['shipping_tc'] = array(
'label' => __('TC Kimlik No', 'woocommerce'),
'placeholder' => _x('Fatura İçin Gerekli', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);

return $fields;
}
/** TC Doğrula  **/
function isTcKimlik($tc){
if(strlen($tc) < 11){ return false; }
if($tc[0] == '0'){ return false; }
$plus = ($tc[0] + $tc[2] + $tc[4] + $tc[6] + $tc[8]) * 7;
$minus = $plus - ($tc[1] + $tc[3] + $tc[5] + $tc[7]);
$mod = $minus % 10;
if($mod != $tc[9]){ return false; }
$all = '';
for($i = 0 ; $i < 10 ; $i++){ $all += $tc[$i]; }
if($all % 10 != $tc[10]){ return false; }

return true;
}
/**  TC Kimlik Noyu Doğrula **/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
$tcno = $_POST['shipping_tc'];
if(!isTcKimlik($tcno))
wc_add_notice( __( 'Lütfen Geçerli TC Kimlik No Girin.' ), 'error' );
}
/** Admin Sipariş Detayında Fatura Bilgilerinde TC No'yu Görebilmesi İçin**/

add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('TC Kimlik No').':</strong> ' . get_post_meta( $order->get_id(), '_shipping_tc', true ) . '</p>';
}
add_filter( 'woocommerce_checkout_fields', 'misha_email_first' );
 
function misha_email_first( $checkout_fields ) {
    $checkout_fields['billing']['shipping_tc']['priority'] = 20;
    return $checkout_fields;
}





// Custom function to display the Billing Address form to registration page
add_action('woocommerce_register_form_start','zk_add_billing_form_to_registration');
function zk_add_billing_form_to_registration(){
    $checkout = WC()->checkout;
    foreach ( $checkout->get_checkout_fields( 'billing' ) as $key => $field ) :
        if($key!='billing_email')
            woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
    endforeach;
}

// Custom function to save Usermeta or Billing Address of registered user
add_action('woocommerce_created_customer','zk_save_billing_address');
function zk_save_billing_address($user_id){
    $address = $_POST;
    foreach ($address as $key => $field){
        // Only billing fields values
        if( strpos( $key, 'billing_' ) !== false ){
            // Condition to add firstname and last name to user meta table
            if($key == 'billing_first_name' || $key == 'billing_last_name'){
                $new_key = str_replace( 'billing_', '', $key );
                update_user_meta( $user_id, $new_key, $_POST[$key] );
            }
            update_user_meta( $user_id, $key, $_POST[$key] );
        }
}
}
add_action( 'woocommerce_checkout_update_user_meta', 'reigel_woocommerce_checkout_update_user_meta', 10, 2 );

// Checking & validation of the additional fields in registration form.
add_action('woocommerce_register_post','zk_validation_billing_address', 10, 3 );
function zk_validation_billing_address( $username, $email, $validation_errors ){
    foreach ($_POST as $key => $field) :
        // Validation: Required fields
        if( strpos( $key, 'billing_' ) !== false ){
            if($key == 'billing_country' && empty($field) ){
                $validation_errors->add( $key.'_error',  __( 'Lütfen ülke seçimi yapınız.', 'woocommerce' ));
            }
            if($key == 'billing_first_name' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Adınızı giriniz.', 'woocommerce' ) );
            }
            if($key == 'billing_last_name' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Soyadınızı giriniz.', 'woocommerce' ) );
            }
            if($key == 'billing_address_1' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Adresinizi giriniz.', 'woocommerce' ) );
            }
            if($key == 'billing_city' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Lütfen şehir seçimi yapınız.', 'woocommerce' ) );
            }
            if($key == 'billing_state' && empty($field) ){
                if(count( WC()->countries->get_states($_POST['billing_country']) ) > 0)
                    $validation_errors->add( $key.'_error', __( 'Mahalle girişi yapınız.', 'woocommerce' ) );
            }
            if($key == 'billing_postcode' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Posta kodu girişi yapınız.', 'woocommerce' ) );
            }
            /*
            if($key == 'billing_email' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter billing email address.', 'woocommerce' ) );
            }
            */
            if($key == 'billing_phone' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Lütfen telefon numaranızı giriniz.', 'woocommerce' ) );
            }
            if($key == 'shipping_tc' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'TC.', 'woocommerce' ) );
            }


        }
    endforeach;
}

add_filter( 'woocommerce_billing_fields', 'sv_required_billing_fields' );
function sv_required_billing_fields( $fields ) {
    $fields['billing_phone']['required'] = true;
    $fields['billing_city']['required'] = true;
    $fields['billing_country']['required'] = true;
    $fields['billing_address_1']['required'] = true;
    return $fields;
}
1

1 Answers

2
votes

Based on your other question answer Save WooCommerce checkout custom field as user meta data, that solves saving field data as user meta data and order meta data.

Now for field validation you will use:

function is_billing_identifier_valid( $tc ){
    if (strlen($tc) < 11 || $tc[0] == '0') 
        return false;
    
    $plus  = ($tc[0] + $tc[2] + $tc[4] + $tc[6] + $tc[8]) * 7;
    $minus = $plus - ($tc[1] + $tc[3] + $tc[5] + $tc[7]);
    $all   = ''; 
    
    if ( $minus % 10 != $tc[9])
        return false;
    
    for ($i = 0 ; $i < 10 ; $i++) 
        $all += $tc[$i];
    
    return $all % 10 != $tc[10] ? false : true;
}

add_action('woocommerce_checkout_process', 'custom_checkout_field_validation');
function custom_checkout_field_validation() {
    if( isset($_POST['billing_identifier']) && ! is_billing_identifier_valid( esc_attr($_POST['billing_identifier']) ) ) {
        wc_add_notice( __( 'Lütfen Geçerli TC Kimlik No Girin.' ), 'error' );
    }
}

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