Automattic\WooCommerce\Internal\Fulfillments\Providers

RoyalMailShippingProvider::try_parse_tracking_numberpublicWC 1.0

Try to parse a Royal Mail tracking number.

Method of the class: RoyalMailShippingProvider{}

No Hooks.

Returns

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

Usage

$RoyalMailShippingProvider = new RoyalMailShippingProvider();
$RoyalMailShippingProvider->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.

RoyalMailShippingProvider::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 ) ); // Normalize input.
	if ( empty( $normalized ) ) {
		return null;
	}

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

	// Check if shipping from UK.
	if ( 'GB' !== $shipping_from ) {
		return null;
	}

	// Check country-specific patterns with enhanced validation.
	if ( $this->validate_country_pattern( $normalized, $shipping_from ) ) {
		$confidence = self::TRACKING_PATTERNS[ $shipping_from ]['confidence'];

		// Apply UPU S10 validation for international formats.
		if ( preg_match( '/^[A-Z]{2}\d{7,9}GB$/', $normalized ) ) {
			if ( FulfillmentUtils::check_s10_upu_format( $normalized ) ) {
				$confidence = min( 98, $confidence + 8 ); // Strong boost for valid UPU.
			}
		}

		// Apply check digit validation for numeric formats.
		if ( preg_match( '/^\d{11,16}$/', $normalized ) ) {
			if ( FulfillmentUtils::validate_mod10_check_digit( $normalized ) ) {
				$confidence = min( 95, $confidence + 5 ); // Boost for valid check digit.
			}
		}

		// Service-specific confidence boosts.
		if ( preg_match( '/^(SD|SF)\d+/', $normalized ) ) {
			$confidence = min( 96, $confidence + 6 ); // Special Delivery/Signed For.
		} elseif ( preg_match( '/^PF\d+/', $normalized ) ) {
			$confidence = min( 94, $confidence + 4 ); // Parcelforce.
		} elseif ( preg_match( '/^(IT|IE|IS)\d+GB$/', $normalized ) ) {
			$confidence = min( 95, $confidence + 5 ); // International tracked services.
		} elseif ( preg_match( '/^(RM|BF)\d+/', $normalized ) ) {
			$confidence = min( 92, $confidence + 3 ); // Standard Royal Mail/Business.
		}

		// Boost confidence for domestic shipments.
		if ( 'GB' === $shipping_to ) {
			$confidence = min( 95, $confidence + 8 );
		}

		// Boost confidence for common destinations (Europe).
		$european_destinations = array( 'FR', 'DE', 'ES', 'IT', 'NL', 'BE', 'IE', 'AT', 'CH', 'PT', 'DK', 'SE', 'NO' );
		if ( in_array( $shipping_to, $european_destinations, true ) ) {
			$confidence = min( 95, $confidence + 3 );
		}

		// Boost for other common destinations.
		$common_destinations = array( 'US', 'CA', 'AU', 'NZ', 'JP', 'SG', 'HK' );
		if ( in_array( $shipping_to, $common_destinations, true ) ) {
			$confidence = min( 93, $confidence + 2 );
		}

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

	return null;
}