commerce_shipping-8.x-2.0-rc2/src/Plugin/Commerce/ShippingMethod/FlatRatePerItem.php

src/Plugin/Commerce/ShippingMethod/FlatRatePerItem.php
<?php

namespace Drupal\commerce_shipping\Plugin\Commerce\ShippingMethod;

use Drupal\commerce_shipping\Attribute\CommerceShippingMethod;
use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce_price\Price;
use Drupal\commerce_shipping\Entity\ShipmentInterface;
use Drupal\commerce_shipping\ShippingRate;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Provides the FlatRatePerItem shipping method.
 */
#[CommerceShippingMethod(
  id: 'flat_rate_per_item',
  label: new TranslatableMarkup('Flat rate per item'),
)]
class FlatRatePerItem extends FlatRate {

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['rate_amount']['#description'] = $this->t('Charged for each quantity of each shipment item.');

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc