public function DynamicRate::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
- tests/
modules/ commerce_shipping_test/ src/ Plugin/ Commerce/ ShippingMethod/ DynamicRate.php, line 23
Class
- DynamicRate
- Provides the Dynamic shipping method. Prices multiplied by weight of package.
Namespace
Drupal\commerce_shipping_test\Plugin\Commerce\ShippingMethodCode
public function calculateRates(ShipmentInterface $shipment) {
$rates = [];
$amount = Price::fromArray($this->configuration['rate_amount']);
$package_type = $shipment
->getPackageType();
if ($package_type === NULL) {
return $rates;
}
$weight = $package_type
->getWeight()
->convert('g')
->getNumber() ?: 1;
$rates[] = new ShippingRate([
'shipping_method_id' => $this->parentEntity
->id(),
'service' => $this->services['default'],
'amount' => $amount
->multiply($weight),
]);
return $rates;
}