You are here

public function FlatRatePerItem::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 FlatRate::calculateRates

File

src/Plugin/Commerce/ShippingMethod/FlatRatePerItem.php, line 33

Class

FlatRatePerItem
Provides the FlatRatePerItem shipping method.

Namespace

Drupal\commerce_shipping\Plugin\Commerce\ShippingMethod

Code

public function calculateRates(ShipmentInterface $shipment) {
  $amount = Price::fromArray($this->configuration['rate_amount']);
  $quantity = $shipment
    ->getTotalQuantity();
  $rates = [];
  $rates[] = new ShippingRate([
    'shipping_method_id' => $this->parentEntity
      ->id(),
    'service' => $this->services['default'],
    'amount' => $amount
      ->multiply($quantity),
  ]);
  return $rates;
}