????? ??????? ?? ??? ?????
-
We are a bit unclear about your exact update. Could you please clarify us with more details probably with the help of screenshots it will be much easier?
Thank you!
allow me to translate and elaborate:
is it possible to have a “national ID number” AKA “National code” field in the checkout?
it’s a 10 digit number, nothing special so far.the tricky part is validation, I have been able to find the snippet for this field, the problem is I cannot incorporate that with “checkout field editor” plugin.
Can you help us make the validation possible?
the code:
<?php function check_national_code($code) { if(!preg_match('/^[0-9]{10}$/',$code)) return false; for($i=0;$i<10;$i++) if(preg_match('/^'.$i.'{10}$/',$code)) return false; for($i=0,$sum=0;$i<9;$i++) $sum+=((10-$i)*intval(substr($code, $i,1))); $ret=$sum%11; $parity=intval(substr($code, 9,1)); if(($ret<2 && $ret==$parity) || ($ret>=2 && $ret==11-$parity)){ return true;} else { return false;} } /** * Add the field to the checkout */ add_action( 'woocommerce_before_order_notes', 'national_code_field' ); function national_code_field( $checkout ) { echo '<div id="national_code_field"><h2>?? ???</h2>'; woocommerce_form_field( 'national_code', array( 'type' => 'text', 'class' => array('my-field-class form-row-wide'), 'label' => '????? ??? ?? ???? ????', 'placeholder' => '????? ... ', 'required' => true, 'validate' => array(), ), $checkout->get_value( 'national_code' )); echo '</div>'; } /** * Process the checkout */ add_action('woocommerce_checkout_process', 'national_code_field_process'); function national_code_field_process() { // Check if set, if its not set add an error. if ( ! $_POST['national_code'] ) { wc_add_notice('???? ?? ??? ??? ????? ???? ????.' , 'error' );} else { // Validates a phone number using a regular expression. if ( 10 != strlen( $_POST['national_code'] ) ) { wc_add_notice( '????? ???? 10 ???????? ????', 'error' ); } else { $n_code=$_POST['national_code']; $res=check_national_code($n_code); if($res!=1) wc_add_notice( '????? ???? ??? ???? ????????', 'error' ); }} } /** * Update the order meta with field value */ add_action( 'woocommerce_checkout_update_order_meta', 'national_code_field_update_order_meta' ); function national_code_field_update_order_meta( $order_id ) { if ( ! empty( $_POST['national_code'] ) ) { update_post_meta( $order_id, 'national_code', sanitize_text_field( $_POST['national_code'] ) ); } } /** * Display field value on the order edit page */ add_action( 'woocommerce_admin_order_data_after_billing_address', 'national_code_field_display_admin_order_meta', 10, 1 ); function national_code_field_display_admin_order_meta($order){ echo '<p><strong> ????? :</strong> ' . get_post_meta( $order->id, 'national_code', true ) . '</p>'; }
Here’s only validation part:
function check_national_code($code) { if(!preg_match('/^[0-9]{10}$/',$code)) return false; for($i=0;$i<10;$i++) if(preg_match('/^'.$i.'{10}$/',$code)) return false; for($i=0,$sum=0;$i<9;$i++) $sum+=((10-$i)*intval(substr($code, $i,1))); $ret=$sum%11; $parity=intval(substr($code, 9,1)); if(($ret<2 && $ret==$parity) || ($ret>=2 && $ret==11-$parity)) return true; return false; }
@golali For custom validation, please try adding the below example code in your theme’s functions.php file.
add_action( 'woocommerce_after_checkout_validation', 'th_custom_validae_national_code', 10, 2 ); function th_custom_validae_national_code( $fields, $errors ){ if(!preg_match('/^[0-9]{10}$/',$fields['national_code'])){ $errors->add( 'validation', 'Enter a valid national code' ); } }
Thank you!
Thank you dear.
This did not work. Is it only checking the field to be a 10-digit number?
“national code” follows certain algorithms that should be checked as well. the code I provided checks them, too.
Is it possible to also incorporated them?
Many thanskPlease try adding the below example code in your theme’s functions.php file.
add_action( 'woocommerce_after_checkout_validation', 'th_custom_validae_national_code', 10, 2 ); function th_custom_validae_national_code( $fields, $errors ){ $present = false; if(!preg_match('/^[0-9]{10}$/',$fields['national_code'])){ $present = true; } for($i=0;$i<10;$i++){ if(preg_match('/^'.$i.'{10}$/',$fields['national_code'])){ $present = true; } } for($i=0,$sum=0;$i<9;$i++){ $sum+=((10-$i)*intval(substr($fields['national_code'], $i,1))); $ret=$sum%11; $parity=intval(substr($fields['national_code'], 9,1)); if(($ret<2 && $ret==$parity) || ($ret>=2 && $ret==11-$parity)){ $present = false; } else { $present = true; } } if($present){ $errors->add( 'validation', 'Enter a valid national code' ); } }
If you are still not able to get it to work, then could you please provide us with an example so that we will get to know how your algorithm works?
Thank you!
thank you very much for the hard work.
I’ve been playing around with this for a while but it doesn’t seem to trigger anything.
Maybe I’m going about it wrong?I made a custom checkout text field, added the id national_code, and then added the snippet you sent to functions.php of my theme.
should I do anything else?
thanksYour issue needs a detailed check. Can you please raise a ticket through our website? We hope our technical team will be able to help you.
Thank you!
Thank you very much. I just made a request on your website under number (14515).
Bests of luckWe hope your issue is resolved.
We are going to mark this thread as resolved.
Thank you!
- The topic ‘????? ??????? ?? ??? ?????’ is closed to new replies.