Automattic\WooCommerce\Internal\Fulfillments
FulfillmentUtils::validate_mod11_check_digit
Validate Mod 11 check digit for tracking numbers (used by DHL).
Method of the class: FulfillmentUtils{}
No Hooks.
Returns
true|false. True if valid, false otherwise.
Usage
$result = FulfillmentUtils::validate_mod11_check_digit( $tracking_number ): bool;
- $tracking_number(string) (required)
- The tracking number.
FulfillmentUtils::validate_mod11_check_digit() FulfillmentUtils::validate mod11 check digit code WC 10.3.3
public static function validate_mod11_check_digit( string $tracking_number ): bool {
if ( ! preg_match( '/^\d+$/', $tracking_number ) || strlen( $tracking_number ) < 2 ) {
return false;
}
$check_digit = (int) substr( $tracking_number, -1 );
$number = substr( $tracking_number, 0, -1 );
$weights = array( 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 );
$sum = 0;
$weight_index = 0;
// Process each digit from right to left.
for ( $i = strlen( $number ) - 1; $i >= 0; $i-- ) {
$digit = (int) $number[ $i ];
$sum += $digit * $weights[ $weight_index % count( $weights ) ];
++$weight_index;
}
$calculated_check = 11 - ( $sum % 11 );
if ( 10 === $calculated_check ) {
$calculated_check = 0;
} elseif ( 11 === $calculated_check ) {
$calculated_check = 5;
}
return $calculated_check === $check_digit;
}