You are here

public function ShipmentManager::selectDefaultRate in Commerce Shipping 8.2

Selects the default rate for the given shipment.

Parameters

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

\Drupal\commerce_shipping\ShippingRate[] $rates: The available rates.

Return value

\Drupal\commerce_shipping\ShippingRate The selected rate.

Overrides ShipmentManagerInterface::selectDefaultRate

File

src/ShipmentManager.php, line 115

Class

ShipmentManager

Namespace

Drupal\commerce_shipping

Code

public function selectDefaultRate(ShipmentInterface $shipment, array $rates) {

  /** @var \Drupal\commerce_shipping\ShippingRate[] $rates */
  $default_rate = reset($rates);
  if ($shipment
    ->getShippingMethodId() && $shipment
    ->getShippingService()) {

    // Select the first rate which matches the shipment's selected
    // shipping method and service.
    foreach ($rates as $rate) {
      if ($shipment
        ->getShippingMethodId() != $rate
        ->getShippingMethodId()) {
        continue;
      }
      if ($shipment
        ->getShippingService() != $rate
        ->getService()
        ->getId()) {
        continue;
      }
      $default_rate = $rate;
      break;
    }
  }
  return $default_rate;
}