public function ShipmentManager::calculateRates in Commerce Shipping 8.2
Calculates rates for the given shipment.
Parameters
\Drupal\commerce_shipping\Entity\ShipmentInterface $shipment: The shipment.
Return value
\Drupal\commerce_shipping\ShippingRate[] The rates.
Overrides ShipmentManagerInterface::calculateRates
File
- src/
ShipmentManager.php, line 79
Class
Namespace
Drupal\commerce_shippingCode
public function calculateRates(ShipmentInterface $shipment) {
$all_rates = [];
/** @var \Drupal\commerce_shipping\ShippingMethodStorageInterface $shipping_method_storage */
$shipping_method_storage = $this->entityTypeManager
->getStorage('commerce_shipping_method');
$shipping_methods = $shipping_method_storage
->loadMultipleForShipment($shipment);
foreach ($shipping_methods as $shipping_method) {
/** @var \Drupal\commerce_shipping\Entity\ShippingMethodInterface $shipping_method */
$shipping_method = $this->entityRepository
->getTranslationFromContext($shipping_method);
$shipping_method_plugin = $shipping_method
->getPlugin();
try {
$rates = $shipping_method_plugin
->calculateRates($shipment);
} catch (\Exception $exception) {
$this->logger
->error('Exception occurred when calculating rates for @name: @message', [
'@name' => $shipping_method
->getName(),
'@message' => $exception
->getMessage(),
]);
continue;
}
// Allow the rates to be altered via code.
$event = new ShippingRatesEvent($rates, $shipping_method, $shipment);
$this->eventDispatcher
->dispatch(ShippingEvents::SHIPPING_RATES, $event);
$rates = $event
->getRates();
$rates = $this
->sortRates($rates);
foreach ($rates as $rate) {
$all_rates[$rate
->getId()] = $rate;
}
}
return $all_rates;
}