Automattic\WooCommerce\Internal\Fulfillments\Providers

DPDShippingProvider::try_parse_tracking_numberpublicWC 1.0

Try to parse a DPD tracking number.

Method of the class: DPDShippingProvider{}

No Hooks.

Returns

Array|null. An array with 'url' and 'ambiguity_score' if valid, null otherwise.

Usage

$DPDShippingProvider = new DPDShippingProvider();
$DPDShippingProvider->try_parse_tracking_number( $tracking_number, $shipping_from, $shipping_to ): ?array;
$tracking_number(string) (required)
The tracking number to parse.
$shipping_from(string) (required)
The country code of the shipping origin.
$shipping_to(string) (required)
The country code of the shipping destination.

DPDShippingProvider::try_parse_tracking_number() code WC 10.3.3

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

	$normalized = strtoupper( preg_replace( '/\s+/', '', $tracking_number ) );
	if ( empty( $normalized ) ) {
		return null;
	}

	$shipping_from = strtoupper( $shipping_from );
	$shipping_to   = strtoupper( $shipping_to );

	// 1. Check international 28-digit format first.
	if ( preg_match( self::INTERNATIONAL_PATTERN, $normalized ) ) {
		if ( in_array( $shipping_from, $this->get_shipping_from_countries(), true ) &&
			in_array( $shipping_to, $this->get_shipping_to_countries(), true ) ) {
			return array(
				'url'             => $this->get_tracking_url( $normalized ),
				'ambiguity_score' => 95,
			);
		}
		return null;
	}

	// 2. Check S10/UPU format (international DPD).
	if ( preg_match( self::S10_PATTERN, $normalized ) ) {
		return array(
			'url'             => $this->get_tracking_url( $normalized ),
			'ambiguity_score' => 90,
		);
	}

	// 3. Check country-specific patterns.
	$validation_result = $this->validate_country_pattern( $normalized, $shipping_from );
	if ( $validation_result && is_array( $validation_result ) ) {
		$confidence = self::TRACKING_PATTERNS[ $shipping_from ]['confidence'];

		// Apply service-specific confidence boost.
		if ( $validation_result['confidence_boost'] > 0 ) {
			$confidence = min( 95, $validation_result['confidence_boost'] );
		}

		// Boost confidence for intra-DPD shipments.
		if ( in_array( $shipping_to, $this->get_shipping_to_countries(), true ) ) {
			$confidence = min( 98, $confidence + 3 );
		}

		// Additional boost for express services.
		if ( 'express' === $validation_result['service'] ) {
			$confidence = min( 98, $confidence + 2 );
		}

		return array(
			'url'             => $this->get_tracking_url( $normalized ),
			'ambiguity_score' => $confidence,
		);
	}

	// 4. Fallback: 12–24 digit numeric.
	if ( preg_match( '/^\d{12,24}$/', $normalized ) ) {
		return array(
			'url'             => $this->get_tracking_url( $normalized ),
			'ambiguity_score' => 60,
		);
	}

	return null;
}