You are here

protected function CommerceFedExPacker::getWeight in Commerce FedEx 8

Gets the weight of the order item.

The weight will be empty if the shippable trait was added but the existing entities were not updated. Add 1 gram as fedex doesn't support 0 weights.

Parameters

\Drupal\commerce_order\Entity\OrderItemInterface $order_item: The order item.

Return value

\Drupal\physical\Weight The weight.

File

src/Packer/CommerceFedExPacker.php, line 109

Class

CommerceFedExPacker
Defines a shipment packer for FedEx.

Namespace

Drupal\commerce_fedex\Packer

Code

protected function getWeight(OrderItemInterface $order_item) {
  $purchasedEntity = $order_item
    ->getPurchasedEntity();
  if ($purchasedEntity
    ->get('weight')
    ->isEmpty()) {
    $weight = new Weight(1, WeightUnit::GRAM);
  }
  else {

    /** @var \Drupal\physical\Plugin\Field\FieldType\MeasurementItem $weight_item */
    $weight_item = $purchasedEntity
      ->get('weight')
      ->first();
    $weight = $weight_item
      ->toMeasurement();
  }
  return $weight;
}