You are here

public function ProfileSubscriber::onProfiles in Commerce Shipping 8.2

Adds the shipping profile to the order profiles.

The shipping profile is assumed to be the same for all shipments.

Parameters

\Drupal\commerce_order\Event\OrderProfilesEvent $event: The order profiles event.

File

src/EventSubscriber/ProfileSubscriber.php, line 27

Class

ProfileSubscriber

Namespace

Drupal\commerce_shipping\EventSubscriber

Code

public function onProfiles(OrderProfilesEvent $event) {
  $order = $event
    ->getOrder();
  if (!$order
    ->hasField('shipments')) {
    return;
  }

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface[] $shipments */
  $shipments = $order
    ->get('shipments')
    ->referencedEntities();
  $shipment = reset($shipments);
  if ($shipment && $shipment
    ->getShippingProfile()) {
    $event
      ->addProfile('shipping', $shipment
      ->getShippingProfile());
  }
}