You are here

public static function FedEx::physicalWeightToFedex in Commerce FedEx 8

Convert physical weight to FedEx weight object.

Parameters

\Drupal\physical\Weight $weight: The weight from Drupal.

Return value

\NicholasCreativeMedia\FedExPHP\Structs\Weight The weight for FedEx.

Throws

\Exception

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

File

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

Class

FedEx
Provides the FedEx shipping method.

Namespace

Drupal\commerce_fedex\Plugin\Commerce\ShippingMethod

Code

public static function physicalWeightToFedex(PhysicalWeight $weight) {
  if (!$weight instanceof PhysicalWeight) {
    throw new \Exception("Invalid units for weight");
  }
  $number = $weight
    ->getNumber();
  switch ($weight
    ->getUnit()) {
    case PhysicalWeightUnits::GRAM:
      $number /= 1000;
      $unit = FedexWeightUnits::VALUE_KG;
      break;
    case PhysicalWeightUnits::KILOGRAM:
      $unit = FedexWeightUnits::VALUE_KG;
      break;
    case PhysicalWeightUnits::OUNCE:
      $number /= 16;
      $unit = FedexWeightUnits::VALUE_LB;
      break;
    case PhysicalWeightUnits::POUND:
      $unit = FedexWeightUnits::VALUE_LB;
      break;
    default:
      throw new \Exception("Invalid Units for Weight");
  }
  return new FedexWeight($unit, $number);
}