Automattic\WooCommerce\Admin\Features\Fulfillments\Providers

DHLShippingProvider::try_parse_tracking_numberpublicWC 1.0

Validates and parses a DHL tracking number.

Method of the class: DHLShippingProvider{}

No Hooks.

Returns

Array|null. Array with tracking URL and score, or null if invalid.

Usage

$DHLShippingProvider = new DHLShippingProvider();
$DHLShippingProvider->try_parse_tracking_number( $tracking_number, $shipping_from, $shipping_to ): ?array;
$tracking_number(string) (required)
The tracking number to validate.
$shipping_from(string) (required)
Origin country code.
$shipping_to(string) (required)
Destination country code.

DHLShippingProvider::try_parse_tracking_number() code WC 10.9.4

public function try_parse_tracking_number( string $tracking_number, string $shipping_from, string $shipping_to ): ?array {
	if ( empty( $tracking_number ) || ! $this->can_ship_from_to( $shipping_from, $shipping_to ) ) {
		return null;
	}

	$tracking_number  = strtoupper( preg_replace( '/\s+/', '', $tracking_number ) ); // Remove spaces and uppercase for consistency.
	$is_major_country = in_array( $shipping_from, $this->major_operation_countries, true ); // Major operation region flag.

	// DHL tracking number patterns with enhanced validation and comments.
	$patterns = array(
		// DHL Express Air Waybill: 10 or 11 digits, with check digit validation.
		'/^\d{10}$/'                                 => function () use ( $tracking_number ) {
			return FulfillmentUtils::validate_mod11_check_digit( $tracking_number ) ? 98 : 90;
		},
		'/^\d{11}$/'                                 => function () use ( $tracking_number ) {
			return FulfillmentUtils::validate_mod11_check_digit( $tracking_number ) ? 98 : 90;
		},

		// DHL Express JJD and JVGL formats.
		'/^JJD\d{10}$/'                              => 98,
		'/^JVGL\d{10}$/'                             => 98,

		// DHL Paket Germany: 12, 14, or 20 digits.
		// Only match 12/14-digit numeric for DHL if both from and to are DE (Germany).
		'/^\d{12}$/'                                 => function () use ( $shipping_from, $shipping_to ) {
			return ( 'DE' === $shipping_from && 'DE' === $shipping_to ) ? 92 : 60;
		},
		'/^\d{14}$/'                                 => function () use ( $shipping_from, $shipping_to ) {
			return ( 'DE' === $shipping_from && 'DE' === $shipping_to ) ? 92 : 60;
		},
		'/^\d{20}$/'                                 => 90,

		// DHL Paket Germany: 3S + 8–12 alphanumeric.
		'/^3S[A-Z0-9]{8,12}$/'                       => 95,

		// DHL eCommerce North America: GM + 16–20 digits.
		'/^GM\d{16,20}$/'                            => function () use ( $shipping_from ) {
			return in_array( $shipping_from, array( 'US', 'CA' ), true ) ? 95 : 80;
		},

		// DHL eCommerce Asia-Pacific: LX, RX, CN, SG, MY, HK, AU, TH + 9 digits + 2 letters.
		'/^(LX|RX|CN|SG|MY|HK|AU|TH)\d{9}[A-Z]{2}$/' => 92,

		// DHL eCommerce US consolidator: 420 + 27–31 digits.
		'/^420\d{23,31}$/'                           => 90,

		// DHL Global Forwarding: 7, 8, or 9 digits (numeric only).
		'/^\d{7,9}$/'                                => 88,

		// DHL Global Forwarding: 1 digit + 2 letters + 4–6 digits.
		'/^\d[A-Z]{2}\d{4,6}$/'                      => 90,

		// DHL Global Forwarding: 3–4 letters + 4–8 digits.
		'/^[A-Z]{3,4}\d{4,8}$/'                      => 88,

		// DHL Same Day: DSD + 8–12 digits.
		'/^DSD\d{8,12}$/'                            => 92,

		// DHL Piece Numbers: JD + 11 digits.
		'/^JD\d{11}$/'                               => 90,

		// DHL Supply Chain: DSC + 10–15 digits.
		'/^DSC\d{10,15}$/'                           => 85,

		// S10/UPU format: 2 letters + 9 digits + 2 letters (used for DHL eCommerce and Packet International).
		'/^[A-Z]{2}\d{9}[A-Z]{2}$/'                  => function () use ( $tracking_number ) {
			return FulfillmentUtils::check_s10_upu_format( $tracking_number ) ? 88 : 75;
		},

		// Fallback: 22 digit numeric (legacy/rare).
		'/^\d{22}$/'                                 => 70,
	);

	foreach ( $patterns as $pattern => $base_score ) {
		if ( preg_match( $pattern, $tracking_number ) ) {
			$score = is_callable( $base_score ) ? $base_score() : $base_score;
			if ( $score > 0 ) {
				return array(
					'url'             => $this->get_tracking_url( $tracking_number ),
					'ambiguity_score' => $score,
				);
			}
		}
	}

	return null;
}