You are here

class OrderItemPercentageOff in Commerce Core 8.2

Provides the percentage off offer for order items.

Plugin annotation


@CommercePromotionOffer(
  id = "order_item_percentage_off",
  label = @Translation("Percentage off each matching product"),
  entity_type = "commerce_order_item",
)

Hierarchy

Expanded class hierarchy of OrderItemPercentageOff

1 file declares its use of OrderItemPercentageOff
PromotionTest.php in modules/promotion/tests/src/Kernel/Entity/PromotionTest.php

File

modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderItemPercentageOff.php, line 18

Namespace

Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer
View source
class OrderItemPercentageOff extends OrderItemPromotionOfferBase {
  use PercentageOffTrait;

  /**
   * {@inheritdoc}
   */
  public function apply(EntityInterface $entity, PromotionInterface $promotion) {
    $this
      ->assertEntity($entity);

    /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
    $order_item = $entity;
    $percentage = $this
      ->getPercentage();
    if ($this->configuration['display_inclusive']) {

      // First, get the adjusted unit price to ensure the order item is not
      // already fully discounted.
      $adjusted_unit_price = $order_item
        ->getAdjustedUnitPrice([
        'promotion',
      ]);

      // The adjusted unit price is already reduced to 0, no need to continue
      // further.
      if ($adjusted_unit_price
        ->isZero()) {
        return;
      }

      // Display-inclusive promotions must first be applied to the unit price.
      $amount = $adjusted_unit_price
        ->multiply($percentage);
      $amount = $this->rounder
        ->round($amount);
      $new_unit_price = $order_item
        ->getUnitPrice()
        ->subtract($amount);
      $order_item
        ->setUnitPrice($new_unit_price);
      $adjustment_amount = $amount
        ->multiply($order_item
        ->getQuantity());
    }
    else {
      $adjusted_total_price = $order_item
        ->getAdjustedTotalPrice([
        'promotion',
      ]);
      $adjustment_amount = $adjusted_total_price
        ->multiply($percentage);
    }
    $adjustment_amount = $this->rounder
      ->round($adjustment_amount);

    // Skip applying the promotion if there's no amount to discount.
    if ($adjustment_amount
      ->isZero()) {
      return;
    }
    $order_item
      ->addAdjustment(new Adjustment([
      'type' => 'promotion',
      'label' => $promotion
        ->getDisplayName() ?: $this
        ->t('Discount'),
      'amount' => $adjustment_amount
        ->multiply('-1'),
      'percentage' => $percentage,
      'source_id' => $promotion
        ->id(),
      'included' => $this->configuration['display_inclusive'],
    ]));
  }

}

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
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
OrderItemPercentageOff::apply public function Applies the offer to the given entity. Overrides PromotionOfferInterface::apply
OrderItemPromotionOfferBase::getConditionOperator public function Gets the condition operator. Overrides OrderItemPromotionOfferInterface::getConditionOperator
OrderItemPromotionOfferBase::getConditions public function Gets the conditions. Overrides OrderItemPromotionOfferInterface::getConditions
OrderItemPromotionOfferBase::setConditions public function Sets the conditions. Overrides OrderItemPromotionOfferInterface::setConditions
PercentageOffTrait::buildConfigurationForm public function
PercentageOffTrait::defaultConfiguration public function
PercentageOffTrait::getPercentage protected function Gets the percentage.
PercentageOffTrait::setConfiguration public function
PercentageOffTrait::submitConfigurationForm public function
PercentageOffTrait::validateConfigurationForm public function
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::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 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::__construct public function Constructs a new PromotionOfferBase object. Overrides PluginBase::__construct 1
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.