You are here

public function ShippingMethodStorage::loadMultipleForShipment in Commerce Shipping 8.2

Loads all eligible shipping methods for the given shipment.

Parameters

\Drupal\commerce_shipping\Entity\ShipmentInterface $shipment: The shipment.

Return value

\Drupal\commerce_shipping\Entity\ShippingMethodInterface[] The shipping methods.

Overrides ShippingMethodStorageInterface::loadMultipleForShipment

File

src/ShippingMethodStorage.php, line 18

Class

ShippingMethodStorage
Defines the shipping method storage.

Namespace

Drupal\commerce_shipping

Code

public function loadMultipleForShipment(ShipmentInterface $shipment) {
  $query = $this
    ->getQuery();
  $query
    ->accessCheck(TRUE)
    ->condition('stores', $shipment
    ->getOrder()
    ->getStoreId())
    ->condition('status', TRUE);
  $result = $query
    ->execute();
  if (empty($result)) {
    return [];
  }
  $shipping_methods = $this
    ->loadMultiple($result);

  // Allow the list of shipping methods to be filtered via code.
  $event = new FilterShippingMethodsEvent($shipping_methods, $shipment);
  $this->eventDispatcher
    ->dispatch(ShippingEvents::FILTER_SHIPPING_METHODS, $event);
  $shipping_methods = $event
    ->getShippingMethods();

  // Evaluate conditions for the remaining ones.
  foreach ($shipping_methods as $shipping_method_id => $shipping_method) {
    if (!$shipping_method
      ->applies($shipment)) {
      unset($shipping_methods[$shipping_method_id]);
    }
  }
  uasort($shipping_methods, [
    $this->entityType
      ->getClass(),
    'sort',
  ]);
  return $shipping_methods;
}