You are here

public static function FedEx::packageTotalWeight in Commerce FedEx 8

Function packageTotalWeight.

Parameters

array $shipment_items: An array of shipment items.

\Drupal\commerce_shipping\Plugin\Commerce\PackageType\PackageTypeInterface $package_type: The commerce_shipping package type.

\Drupal\physical\Weight|null $adjustment: Any weight adjustment to add or subtract.

Return value

\NicholasCreativeMedia\FedExPHP\Structs\Weight The total weight of the package.

1 call to FedEx::packageTotalWeight()
DryIcePlugin::adjustPackage in modules/dry_ice/src/Plugin/Commerce/FedEx/DryIcePlugin.php
Adjust a package based on the items, shipment and profile.

File

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

Class

FedEx
Provides the FedEx shipping method.

Namespace

Drupal\commerce_fedex\Plugin\Commerce\ShippingMethod

Code

public static function packageTotalWeight(array $shipment_items, PackageTypeInterface $package_type, PhysicalWeight $adjustment = NULL) {
  if (empty($shipment_items)) {
    return new FedexWeight('LB', 0);
  }
  $storage = \Drupal::entityTypeManager()
    ->getStorage('commerce_order_item');
  $unit = $storage
    ->load(reset($shipment_items)
    ->getOrderItemId())
    ->getPurchasedEntity()->weight
    ->first()
    ->toMeasurement()
    ->getUnit();
  $total_weight = new PhysicalWeight("0", $unit);
  foreach ($shipment_items as $shipment_item) {

    /* @var ShipmentItem $shipment_item */

    /* @var \Drupal\commerce_order\Entity\OrderItem $order_item */
    $order_item = $storage
      ->load($shipment_item
      ->getOrderItemId());
    $purchased_entity = $order_item
      ->getPurchasedEntity();
    $order_item_weight = $purchased_entity->weight
      ->first()
      ->toMeasurement()
      ->convert($unit)
      ->multiply($order_item
      ->getQuantity());
    $total_weight = $total_weight
      ->add($order_item_weight);
  }
  $total_weight = $total_weight
    ->add($package_type
    ->getWeight()
    ->convert($unit));
  if ($adjustment) {
    $total_weight = $total_weight
      ->add($adjustment
      ->convert($unit));
  }
  return static::physicalWeightToFedex($total_weight);
}