Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ravahdati/97ad71c3757f45756dd765dac2a48b22 to your computer and use it in GitHub Desktop.
Save ravahdati/97ad71c3757f45756dd765dac2a48b22 to your computer and use it in GitHub Desktop.
Add Iranian National Code In The Woocommerce Checkout (Save & Display In Admin)
/**
* @snippet Add iranian national code in the checkout with display 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');
/**
* Display the national code in the admin order meta
*
* @param WC_Order $order The order object for which the national code is displayed.
*/
function checkout_field_display_admin_order_meta( $order )
{
echo '<p><strong>' . __('National Code', 'woocommerce') . ':</strong> ' . get_post_meta( $order->get_id(), 'billing_national_code', true ) . '</p>';
}
add_action('woocommerce_admin_order_data_after_billing_address', 'checkout_field_display_admin_order_meta', 10, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment