You are here

class ShipmentFixedAmountOff in Commerce Shipping 8.2

Provides the fixed amount off offer for shipments.

Plugin annotation


@CommercePromotionOffer(
  id = "shipment_fixed_amount_off",
  label = @Translation("Fixed amount off the shipment amount"),
  entity_type = "commerce_order"
)

Hierarchy

Expanded class hierarchy of ShipmentFixedAmountOff

File

src/Plugin/Commerce/PromotionOffer/ShipmentFixedAmountOff.php, line 19

Namespace

Drupal\commerce_shipping\Plugin\Commerce\PromotionOffer
View source
class ShipmentFixedAmountOff extends ShipmentPromotionOfferBase {
  use FixedAmountOffTrait;

  /**
   * {@inheritdoc}
   */
  public function applyToShipment(ShipmentInterface $shipment, PromotionInterface $promotion) {
    $amount = $this
      ->getAmount();
    if ($amount
      ->getCurrencyCode() != $shipment
      ->getAmount()
      ->getCurrencyCode()) {
      return;
    }

    // The offer amount can't be larger than the remaining shipment amount,
    // to avoid a negative total.
    $remaining_amount = $shipment
      ->getAdjustedAmount();
    if ($amount
      ->greaterThan($remaining_amount)) {
      $amount = $remaining_amount;
    }

    // Display-inclusive promotions must first be applied to the amount.
    if ($this
      ->isDisplayInclusive()) {
      $new_shipment_amount = $shipment
        ->getAmount()
        ->subtract($amount);
      $shipment
        ->setAmount($new_shipment_amount);
    }
    $shipment
      ->addAdjustment(new Adjustment([
      'type' => 'shipping_promotion',
      'label' => $promotion
        ->getDisplayName() ?: $this
        ->t('Discount'),
      'amount' => $amount
        ->multiply('-1'),
      'source_id' => $promotion
        ->id(),
      'included' => $this
        ->isDisplayInclusive(),
    ]));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FixedAmountOffTrait::buildConfigurationForm public function
FixedAmountOffTrait::defaultConfiguration public function
FixedAmountOffTrait::getAmount protected function Gets the offer amount.
FixedAmountOffTrait::submitConfigurationForm public function
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PromotionOfferBase::$rounder protected property The rounder.
PromotionOfferBase::assertEntity protected function Asserts that the given entity is of the expected type.
PromotionOfferBase::clear public function Allows an offer to clean up any modifications done to the given entity. Overrides PromotionOfferInterface::clear 1
PromotionOfferBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
PromotionOfferBase::getEntityTypeId public function Gets the offer entity type ID. Overrides PromotionOfferInterface::getEntityTypeId
PromotionOfferBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
PromotionOfferBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 2
ShipmentFixedAmountOff::applyToShipment public function Applies the offer to the given shipment. Overrides ShipmentPromotionOfferBase::applyToShipment
ShipmentPromotionOfferBase::$entityUuidMapper protected property The entity UUID mapper.
ShipmentPromotionOfferBase::$shippingOrderManager protected property The shipping order manager.
ShipmentPromotionOfferBase::appliesToShipment protected function Checks whether the promotion can be applied to the given shipment.
ShipmentPromotionOfferBase::apply public function Applies the offer to the given entity. Overrides PromotionOfferInterface::apply
ShipmentPromotionOfferBase::create public static function Creates an instance of the plugin. Overrides PromotionOfferBase::create
ShipmentPromotionOfferBase::getShippingMethodUuids protected function Gets the configured shipping method UUIDs.
ShipmentPromotionOfferBase::isDisplayInclusive public function Gets whether the offer is display inclusive. Overrides ShipmentPromotionOfferInterface::isDisplayInclusive
ShipmentPromotionOfferBase::__construct public function Constructs a new ShipmentPromotionOfferBase object. Overrides PromotionOfferBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.