You are here

public function ShipmentManager::applyRate in Commerce Shipping 8.2

Applies the given shipping rate to the given shipment.

Parameters

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

\Drupal\commerce_shipping\ShippingRate $rate: The shipping rate.

Overrides ShipmentManagerInterface::applyRate

File

src/ShipmentManager.php, line 65

Class

ShipmentManager

Namespace

Drupal\commerce_shipping

Code

public function applyRate(ShipmentInterface $shipment, ShippingRate $rate) {
  $shipping_method_storage = $this->entityTypeManager
    ->getStorage('commerce_shipping_method');

  /** @var \Drupal\commerce_shipping\Entity\ShippingMethodInterface $shipping_method */
  $shipping_method = $shipping_method_storage
    ->load($rate
    ->getShippingMethodId());
  $shipping_method_plugin = $shipping_method
    ->getPlugin();
  if (empty($shipment
    ->getPackageType())) {
    $shipment
      ->setPackageType($shipping_method_plugin
      ->getDefaultPackageType());
  }
  $shipping_method_plugin
    ->selectRate($shipment, $rate);
}