You are here

public function CommerceFedExPacker::pack in Commerce FedEx 8

Packs the given order.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The order.

\Drupal\profile\Entity\ProfileInterface $shipping_profile: The shipping profile.

Return value

\Drupal\commerce_shipping\ProposedShipment[] The proposed shipments.

Overrides DefaultPacker::pack

File

src/Packer/CommerceFedExPacker.php, line 52

Class

CommerceFedExPacker
Defines a shipment packer for FedEx.

Namespace

Drupal\commerce_fedex\Packer

Code

public function pack(OrderInterface $order, ProfileInterface $shipping_profile) {
  $shipments = [
    [
      'title' => $this
        ->t('Primary Shipment'),
      'items' => [],
    ],
  ];
  foreach ($this
    ->getOrderItems($order, $shipping_profile) as $order_item) {
    $purchased_entity = $order_item
      ->getPurchasedEntity();

    // Ship only shippable purchasable entity types.
    if (!$purchased_entity || !$purchased_entity
      ->hasField('weight')) {
      continue;
    }
    $quantity = $order_item
      ->getQuantity();
    $shipments[0]['items'][] = new ShipmentItem([
      'order_item_id' => $order_item
        ->id(),
      'title' => $order_item
        ->getTitle(),
      'quantity' => $quantity,
      'weight' => $this
        ->getWeight($order_item)
        ->multiply($quantity),
      'declared_value' => $order_item
        ->getUnitPrice()
        ->multiply($quantity),
    ]);
  }
  $proposed_shipments = [];
  foreach ($shipments as $shipment) {
    if (!empty($shipment['items'])) {
      $proposed_shipments[] = new ProposedShipment([
        'type' => $this
          ->getShipmentType($order),
        'order_id' => $order
          ->id(),
        'title' => $shipment['title'],
        'items' => $shipment['items'],
        'shipping_profile' => $shipping_profile,
      ]);
    }
  }
  return $proposed_shipments;
}