You are here

public function USPSInternational::calculateRates in Commerce USPS 8

Calculates rates for the given shipment.

Parameters

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

Return value

\Drupal\commerce_shipping\ShippingRate[] The rates.

Overrides ShippingMethodInterface::calculateRates

File

src/Plugin/Commerce/ShippingMethod/USPSInternational.php, line 63

Class

USPSInternational
Provides the USPS international shipping method.

Namespace

Drupal\commerce_usps\Plugin\Commerce\ShippingMethod

Code

public function calculateRates(ShipmentInterface $shipment) {

  // Only attempt to collect rates if an address exists on the shipment.
  if ($shipment
    ->getShippingProfile()
    ->get('address')
    ->isEmpty()) {
    return [];
  }

  // Do not attempt to collect rates for US addresses.
  if ($shipment
    ->getShippingProfile()
    ->get('address')->country_code == 'US') {
    return [];
  }

  // Make sure a package type is set on the shipment.
  $this
    ->setPackageType($shipment);
  return $this->uspsRateService
    ->getRates($shipment, $this->parentEntity);
}