Automattic\WooCommerce\Internal\Fulfillments\Providers

AmazonLogisticsShippingProvider::try_parse_tracking_numberpublicWC 1.0

Validates and parses an Amazon Logistics tracking number.

Method of the class: AmazonLogisticsShippingProvider{}

No Hooks.

Returns

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

Usage

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

AmazonLogisticsShippingProvider::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 ) || ! $this->can_ship_from_to( $shipping_from, $shipping_to ) ) {
		return null;
	}

	$tracking_number = strtoupper( preg_replace( '/\s+/', '', $tracking_number ) );

	// Amazon Logistics tracking number patterns with region/service differentiation.
	$patterns = array(
		// North America patterns.
		'/^TBA\d{12}$/'       => fn() => 'US' === $shipping_from ? 100 : 95, // US standard format.
		'/^TBC\d{12}$/'       => fn() => 'CA' === $shipping_from ? 100 : 90, // Canada standard format.
		'/^TBM\d{12}$/'       => fn() => 'MX' === $shipping_from ? 100 : 85, // Mexico standard format.

		// European patterns.
		'/^CC\d{12}$/'        => fn() => in_array( $shipping_from, array( 'FR', 'BE', 'NL', 'DE' ), true ) ? 95 : 80, // Continental Europe.
		'/^GBA\d{12}$/'       => fn() => 'GB' === $shipping_from ? 100 : 85, // United Kingdom.
		'/^UK\d{10}$/'        => fn() => 'GB' === $shipping_from ? 100 : 85, // United Kingdom.
		'/^W[A-Z]\d{9}GB$/'   => fn() => 'GB' === $shipping_from ? 99 : 85, // Amazon UK specific pattern.
		'/^[A-Z]{2}\d{9}GB$/' => fn() => 'GB' === $shipping_from ? 92 : 75, // United Kingdom.
		'/^AM\d{12}$/'        => fn() => in_array( $shipping_from, array( 'DE', 'FR', 'IT', 'ES' ), true ) ? 95 : 80, // Amazon Europe.
		'/^D\d{13}$/'         => fn() => 'DE' === $shipping_from ? 95 : 75, // Germany specific.

		// Asia-Pacific patterns.
		'/^RB\d{12}$/'        => fn() => in_array( $shipping_from, array( 'CN', 'HK' ), true ) ? 95 : 75, // China/Hong Kong.
		'/^ZZ\d{12}$/'        => fn() => 'AU' === $shipping_from ? 100 : 80, // Australia.
		'/^ZX\d{12}$/'        => fn() => 'IN' === $shipping_from ? 100 : 85, // India.
		'/^JP\d{12}$/'        => fn() => 'JP' === $shipping_from ? 100 : 85, // Japan.
		'/^SG\d{12}$/'        => fn() => 'SG' === $shipping_from ? 100 : 85, // Singapore.

		// Amazon Fresh/Whole Foods.
		'/^AF\d{12}$/'        => fn() => 'US' === $shipping_from ? 98 : 80, // Amazon Fresh US.
		'/^WF\d{12}$/'        => fn() => 'US' === $shipping_from ? 98 : 80, // Whole Foods US.

		// Amazon Business.
		'/^AB\d{12}$/'        => fn() => in_array( $shipping_from, array( 'US', 'GB', 'DE', 'FR' ), true ) ? 95 : 80, // Amazon Business.

		// Legacy and alternative formats.
		'/^TB[A-Z]\d{11}$/'   => fn() => in_array( $shipping_from, array( 'US', 'CA', 'MX' ), true ) ? 90 : 70, // Variable third character.
		'/^AZ\d{12}$/'        => fn() => in_array( $shipping_from, array( 'US', 'GB', 'DE' ), true ) ? 88 : 75, // Alternative format.

		// Amazon Pantry/Subscribe & Save.
		'/^AP\d{12}$/'        => fn() => 'US' === $shipping_from ? 90 : 75, // Pantry US.
		'/^SS\d{12}$/'        => fn() => 'US' === $shipping_from ? 90 : 75, // Subscribe & Save US.

		// Fallback: 15-20 character Amazon codes (future-proof, low confidence).
		'/^[A-Z0-9]{15,20}$/' => fn() => 60,
	);

	foreach ( $patterns as $pattern => $score_callback ) {
		if ( preg_match( $pattern, $tracking_number ) ) {
			return array(
				'url'             => $this->get_tracking_url( $tracking_number ),
				'ambiguity_score' => $score_callback(),
			);
		}
	}

	return null;
}