You are here

public function USPSBase::getTrackingUrl in Commerce USPS 8

Returns a tracking URL for USPS shipments.

Parameters

\Drupal\commerce_shipping\Entity\ShipmentInterface $shipment: The commerce shipment.

Return value

mixed The URL object or FALSE.

Overrides SupportsTrackingInterface::getTrackingUrl

File

src/Plugin/Commerce/ShippingMethod/USPSBase.php, line 246

Class

USPSBase

Namespace

Drupal\commerce_usps\Plugin\Commerce\ShippingMethod

Code

public function getTrackingUrl(ShipmentInterface $shipment) {
  $code = $shipment
    ->getTrackingCode();
  if (!empty($code)) {

    // If the tracking code token exists, replace it with the code.
    if (strstr($this->configuration['options']['tracking_url'], '[tracking_code]')) {
      $url = str_replace('[tracking_code]', $code, $this->configuration['options']['tracking_url']);
    }
    else {

      // Otherwise, append the tracking code to the end of the URL.
      $url = $this->configuration['options']['tracking_url'] . $code;
    }
    return Url::fromUri($url);
  }
  return FALSE;
}