Last active
April 21, 2024 14:22
-
-
Save ravahdati/0967e5c70df5a2f9c42a8d73cdbb001a to your computer and use it in GitHub Desktop.
Add Iranian National Code In The Woocommerce Checkout (Save, Edit & Display In Admin)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @snippet Add iranian national code in the checkout with editing in admin | |
* @author Rasool Vahdati | |
* @compatible WooCommerce 6 | |
*/ | |
/** | |
* Check the validity of an Iranian national code | |
* | |
* @param string $code The national code to be validated. | |
* @return bool True if the code is valid, false otherwise. | |
*/ | |
function check_national_code( $code ) | |
{ | |
// Check if the input code is a 10-digit number | |
if (!preg_match('/^[0-9]{10}$/', $code)) { | |
return false; | |
} | |
// Check for repetitive patterns like "0000000000" | |
for ($i = 0; $i < 10; $i++) { | |
if (preg_match('/^'.$i.'{10}$/', $code)) { | |
return false; | |
} | |
} | |
// Calculate the sum based on the algorithm | |
for ($i = 0, $sum = 0; $i < 9; $i++) { | |
$sum += ((10 - $i) * intval(substr($code, $i, 1))); | |
} | |
// Validate the code using modulus operation | |
$ret = $sum % 11; | |
$parity = intval(substr($code, 9, 1)); | |
if (($ret < 2 && $ret == $parity) || ($ret >= 2 && $ret == 11 - $parity)) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Add custom national code field to WooCommerce payment page | |
* This function adds an input field for users to enter their national code during checkout. | |
*/ | |
function add_custom_national_code_field() | |
{ | |
echo '<div class="form-row form-row-wide woocommerce-additional-fields__field-wrapper"> | |
<label for="billing_national_code">' . __('National Code', 'woocommerce') . ' <span class="required">*</span></label> | |
<span class="woocommerce-input-wrapper"><input type="text" class="input-text" name="billing_national_code" id="billing_national_code" value="' . esc_attr( isset( $_POST['billing_national_code'] ) ? $_POST['billing_national_code'] : '') . '" /></span> | |
</div>'; | |
} | |
add_action('woocommerce_after_checkout_billing_form', 'add_custom_national_code_field'); | |
/** | |
* Validate custom national code field during checkout process | |
* This function validates the entered national code during the checkout process. | |
*/ | |
function validate_custom_national_code_field() | |
{ | |
$national_code = isset( $_POST['billing_national_code'] ) ? sanitize_text_field( $_POST['billing_national_code'] ) : ''; | |
if (!empty( $national_code ) && !check_national_code( $national_code ) ) { | |
wc_add_notice( __('The entered national code is not valid.', 'woocommerce'), 'error'); | |
} | |
} | |
add_action('woocommerce_checkout_process', 'validate_custom_national_code_field'); | |
/** | |
* Save custom national code field in the order meta | |
* | |
* @param int $order_id The ID of the order to which the national code belongs. | |
*/ | |
function save_custom_national_code_field( $order_id ) | |
{ | |
if ( !empty( $_POST['billing_national_code'] ) ) { | |
update_post_meta( $order_id, 'billing_national_code', sanitize_text_field( $_POST['billing_national_code'] ) ); | |
} | |
} | |
add_action('woocommerce_checkout_update_order_meta', 'save_custom_national_code_field'); | |
/** | |
* Add national code field to billing fields section in order details in admin | |
* | |
* @param array $fields The fields in the billing section. | |
* @return array The modified fields array. | |
*/ | |
function add_national_code_to_admin_order_billing_fields($fields) | |
{ | |
$fields['national_code'] = array( | |
'label' => __('National Code', 'your-theme-domain'), | |
'show' => true, | |
'value' => get_post_meta( get_the_ID(), 'billing_national_code', true ), | |
); | |
return $fields; | |
} | |
add_filter('woocommerce_admin_billing_fields', 'add_national_code_to_admin_order_billing_fields'); | |
/** | |
* Save national code field in the order meta from admin order edit page. | |
* | |
* @param int $order_id The ID of the order. | |
*/ | |
function save_custom_national_code_field_admin( $order_id ) | |
{ | |
if ( !empty( $_POST['billing_national_code'] ) ) { | |
update_post_meta($order_id, 'billing_national_code', sanitize_text_field( $_POST['billing_national_code'] ) ); | |
} | |
} | |
add_action('woocommerce_process_shop_order_meta', 'save_custom_national_code_field_admin'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment