Automattic\WooCommerce\Internal\Fulfillments\Providers
UPSShippingProvider::try_parse_tracking_number
Try to parse the tracking number with additional parameters.
Method of the class: UPSShippingProvider{}
No Hooks.
Returns
Array|null. The tracking URL with ambiguity score, or null if parsing fails.
Usage
$UPSShippingProvider = new UPSShippingProvider(); $UPSShippingProvider->try_parse_tracking_number( $tracking_number, $shipping_from, $shipping_to ): ?array;
- $tracking_number(string) (required)
- The tracking number.
- $shipping_from(string) (required)
- The country code from which the shipment is sent.
- $shipping_to(string) (required)
- The country code to which the shipment is sent.
UPSShippingProvider::try_parse_tracking_number() UPSShippingProvider::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 ) || ! $this->can_ship_from_to( $shipping_from, $shipping_to ) ) {
return null;
}
$tracking_number = strtoupper( $tracking_number );
$is_domestic_shipping = $shipping_from === $shipping_to;
// UPS tracking number patterns (ordered by confidence).
$patterns = array(
// 1Z format (standard UPS) - 18 chars, check digit validation.
'/^1Z[0-9A-Z]{16}$/' => function () use ( $tracking_number ) {
return FulfillmentUtils::validate_ups_1z_check_digit( $tracking_number ) ? 100 : 95;
},
// Numeric only: 12 digits (common for UPS Air/Ground, with mod10 check digit).
'/^\d{12}$/' => function () use ( $tracking_number ) {
return FulfillmentUtils::validate_mod10_check_digit( $tracking_number ) ? 90 : 80;
},
// Numeric only: 9 or 10 digits (legacy/freight).
'/^\d{10}$/' => 75,
'/^\d{9}$/' => 70,
// T, H, or V prefix + 10 digits (special international/freight).
'/^[THV]\d{10}$/' => 85,
// UPS InfoNotice (J + 10 digits).
'/^J\d{10}$/' => 80,
// UPS Mail Innovations Parcel ID (MI + 6 digits + up to 22 alphanum).
'/^MI\d{6}[A-Z0-9]{6,22}$/' => 80,
// USPS Delivery Confirmation (Mail Innovations, 22–34 digits).
'/^9\d{21,33}$/' => function () use ( $shipping_from ) {
return in_array( $shipping_from, array( 'US', 'CA' ), true ) ? 85 : 70;
},
// UPU S10 format (international, e.g. 'AA123456789CC').
'/^[A-Z]{2}\d{9}[A-Z]{2}$/' => function () use ( $shipping_from ) {
return in_array( $shipping_from, $this->domestic_but_international_tracking, true ) ? 80 : 65;
},
// Long mail format (22 digits).
'/^\d{22}$/' => 60,
);
$match = false;
$ambiguity_score = 0;
foreach ( $patterns as $pattern => $score ) {
if ( preg_match( $pattern, $tracking_number ) ) {
$match = true;
$ambiguity_score = is_callable( $score ) ? $score() : $score;
break;
}
}
// Boost score for domestic-but-international-tracking countries.
if ( $match && $is_domestic_shipping && in_array( $shipping_from, $this->domestic_but_international_tracking, true ) ) {
$ambiguity_score += 5;
}
return $match ? array(
'url' => $this->get_tracking_url( $tracking_number ),
'ambiguity_score' => $ambiguity_score,
) : null;
}