
| Current Path : /var/www/html/store/web/modules/contrib/commerce_shipping/src/EventSubscriber/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/html/store/web/modules/contrib/commerce_shipping/src/EventSubscriber/ProfileSubscriber.php |
<?php
namespace Drupal\commerce_shipping\EventSubscriber;
use Drupal\commerce_order\Event\OrderProfilesEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProfileSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
'commerce_order.profiles' => ['onProfiles'],
];
}
/**
* Adds the shipping profile to the order profiles.
*
* The shipping profile is assumed to be the same for all shipments.
*
* @param \Drupal\commerce_order\Event\OrderProfilesEvent $event
* The order profiles event.
*/
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());
}
}
}