Automattic\WooCommerce\Internal\Fulfillments\Providers

CanadaPostShippingProvider::try_parse_tracking_numberpublicWC 1.0

Try to parse a Canada Post tracking number.

Method of the class: CanadaPostShippingProvider{}

No Hooks.

Returns

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

Usage

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

CanadaPostShippingProvider::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 Canada.
	if ( 'CA' !== $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{9}CA$/', $normalized ) ) {
			if ( FulfillmentUtils::check_s10_upu_format( $normalized ) ) {
				$confidence = min( 98, $confidence + 6 ); // Strong boost for valid UPU.
			}
		} elseif ( preg_match( '/^[A-Z]{2}\d{9}[A-Z]{2}$/', $normalized ) ) {
			// Apply S10/UPU fallback with lower confidence.
			if ( FulfillmentUtils::check_s10_upu_format( $normalized ) ) {
				$confidence = min( 94, $confidence + 2 ); // Lower boost for inbound S10.
			}
		}

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

		// Service-specific confidence boosts.
		if ( preg_match( '/^(XP|EX|PR)\d+/', $normalized ) ) {
			$confidence = min( 96, $confidence + 4 ); // Express/Priority services.
		} elseif ( preg_match( '/^(RM|CM)\d+/', $normalized ) ) {
			$confidence = min( 95, $confidence + 3 ); // Registered/Certified.
		} elseif ( preg_match( '/^(FD|PO|CP|SM)\d+/', $normalized ) ) {
			$confidence = min( 94, $confidence + 2 ); // Special services.
		}

		// Boost confidence for domestic shipments.
		if ( 'CA' === $shipping_to ) {
			$confidence = min( 98, $confidence + 3 );
		}

		// Boost for North American destinations.
		if ( in_array( $shipping_to, array( 'US', 'MX' ), true ) ) {
			$confidence = min( 95, $confidence + 2 );
		}

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

	return null;
}