You are here

protected function FedEx::getShipmentTotalVolume in Commerce FedEx 8

Gets the shipment's total volume in the same units as the package type.

Parameters

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

Return value

float The volume.

File

src/Plugin/Commerce/ShippingMethod/FedEx.php, line 972

Class

FedEx
Provides the FedEx shipping method.

Namespace

Drupal\commerce_fedex\Plugin\Commerce\ShippingMethod

Code

protected function getShipmentTotalVolume(ShipmentInterface $shipment) {
  $order_item_storage = \Drupal::entityTypeManager()
    ->getStorage('commerce_order_item');
  $order_item_ids = [];
  foreach ($shipment
    ->getItems() as $shipment_item) {
    $order_item_ids[] = $shipment_item
      ->getOrderItemId();
  }
  $order_items = $order_item_storage
    ->loadMultiple($order_item_ids);
  $unit = $shipment
    ->getPackageType()
    ->getHeight()
    ->getUnit();
  $total_volume = 0;
  foreach ($order_items as $orderItem) {

    /** @var \Drupal\commerce_order\Entity\OrderItem $orderItem */
    $purchased_entity = $orderItem
      ->getPurchasedEntity();
    if ($purchased_entity
      ->hasField('dimensions') && !$purchased_entity
      ->get('dimensions')
      ->isEmpty()) {

      /** @var \Drupal\physical\Plugin\Field\FieldType\DimensionsItem $dimensions */
      $dimensions = $purchased_entity
        ->get('dimensions')
        ->first();
      $volume = $dimensions
        ->getHeight()
        ->convert($unit)
        ->multiply($dimensions
        ->getWidth()
        ->convert($unit)
        ->getNumber())
        ->multiply($dimensions
        ->getLength()
        ->convert($unit)
        ->getNumber())
        ->getNumber();
      $total_volume += (double) $volume * $orderItem
        ->getQuantity();
    }
  }
  return $total_volume;
}