You are here

public function ShippingByWeight::calculateRates in Commerce Shipping Weight Tariff 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Commerce/ShippingMethod/ShippingByWeight.php \Drupal\commerce_shipping_weight_tariff\Plugin\Commerce\ShippingMethod\ShippingByWeight::calculateRates()

Calculates shipment rate depending on weight conditions.

Parameters

\Drupal\commerce_shipping\Entity\ShipmentInterface $shipment: Shipment interface entity.

Return value

array Returns calculated rates.

Overrides ShippingMethodInterface::calculateRates

File

src/Plugin/Commerce/ShippingMethod/ShippingByWeight.php, line 127

Class

ShippingByWeight
Provides the FlatRatePerItem shipping method.

Namespace

Drupal\commerce_shipping_weight_tariff\Plugin\Commerce\ShippingMethod

Code

public function calculateRates(ShipmentInterface $shipment) {

  // Rate IDs aren't used in a flat rate scenario because there's always a
  // single rate per plugin, and there's no support for purchasing rates.
  $amount = $this->configuration['base_rate_amount'];
  $shipment_tax_by_weight = $this->stateService
    ->get('order_' . $shipment
    ->getOrderId() . '_weight_condition');
  $this->stateService
    ->delete('order_' . $shipment
    ->getOrderId() . '_weight_condition');

  // Calculate shipping amount.
  $shipping_tax_value = $shipment_tax_by_weight['condition_price'] + $amount['number'];

  // Set shipping amount.
  $amount = new Price((string) $shipping_tax_value, $amount['currency_code']);
  $rates = [];

  // Set shipping rate.
  $rates[] = new ShippingRate([
    'shipping_method_id' => $this->parentEntity
      ->id(),
    'service' => $this->services['default'],
    'amount' => $amount,
  ]);
  return $rates;
}