class OrderItemFixedAmountOff in Commerce Currency Resolver 8
Provides the percentage off offer for order items with multicurrency support.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\PromotionOfferBase implements PromotionOfferInterface, ContainerFactoryPluginInterface
- class \Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\OrderItemPromotionOfferBase implements OrderItemPromotionOfferInterface
- class \Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\OrderItemFixedAmountOff uses FixedAmountOffTrait
- class \Drupal\commerce_currency_resolver\Plugin\Commerce\PromotionOffer\OrderItemFixedAmountOff uses CommerceCurrencyResolverAmountTrait
- class \Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\OrderItemFixedAmountOff uses FixedAmountOffTrait
- class \Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\OrderItemPromotionOfferBase implements OrderItemPromotionOfferInterface
- class \Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\PromotionOfferBase implements PromotionOfferInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of OrderItemFixedAmountOff
See also
\Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\OrderItemFixedAmountOff
1 file declares its use of OrderItemFixedAmountOff
- commerce_currency_resolver.module in ./
commerce_currency_resolver.module - Main module file.
File
- src/
Plugin/ Commerce/ PromotionOffer/ OrderItemFixedAmountOff.php, line 16
Namespace
Drupal\commerce_currency_resolver\Plugin\Commerce\PromotionOfferView source
class OrderItemFixedAmountOff extends CommerceOrderItemFixedAmountOff {
use CommerceCurrencyResolverAmountTrait;
/**
* {@inheritdoc}
*/
public function apply(EntityInterface $entity, PromotionInterface $promotion) {
// Nothing to do. Go to parent.
if (!$this
->shouldCurrencyRefresh($this
->getAmount()
->getCurrencyCode())) {
return parent::apply($entity, $promotion);
}
$this
->assertEntity($entity);
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
$order_item = $entity;
$total_price = $order_item
->getTotalPrice();
// Convert prices.
$amount = $this
->getPrice($this
->getAmount());
if ($this->configuration['display_inclusive']) {
// Display-inclusive promotions must first be applied to the unit price.
$unit_price = $order_item
->getUnitPrice();
if ($amount
->greaterThan($unit_price)) {
// Don't reduce the unit price past zero.
$amount = $unit_price;
}
$new_unit_price = $unit_price
->subtract($amount);
$order_item
->setUnitPrice($new_unit_price);
$adjustment_amount = $amount
->multiply($order_item
->getQuantity());
$adjustment_amount = $this->rounder
->round($adjustment_amount);
}
else {
$adjustment_amount = $amount
->multiply($order_item
->getQuantity());
$adjustment_amount = $this->rounder
->round($adjustment_amount);
if ($adjustment_amount
->greaterThan($total_price)) {
// Don't reduce the order item total price past zero.
$adjustment_amount = $total_price;
}
}
$order_item
->addAdjustment(new Adjustment([
'type' => 'promotion',
'label' => $promotion
->getDisplayName() ?: $this
->t('Discount'),
'amount' => $adjustment_amount
->multiply('-1'),
'source_id' => $promotion
->id(),
'included' => $this->configuration['display_inclusive'],
]));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CommerceCurrencyResolverAmountTrait:: |
public | function | ||
CommerceCurrencyResolverAmountTrait:: |
public | function | Get resolved currency. | |
CommerceCurrencyResolverAmountTrait:: |
public | function | Get price based on currency price and target currency. | |
CommerceCurrencyResolverAmountTrait:: |
public | function | Do not run conditions currency conversion on specific conditions. | |
CommerceCurrencyResolverAmountTrait:: |
public | function | ||
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FixedAmountOffTrait:: |
public | function | ||
FixedAmountOffTrait:: |
protected | function | Gets the offer amount. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
OrderItemFixedAmountOff:: |
public | function |
Applies the offer to the given entity. Overrides OrderItemFixedAmountOff:: |
|
OrderItemPromotionOfferBase:: |
public | function |
Gets the condition operator. Overrides OrderItemPromotionOfferInterface:: |
|
OrderItemPromotionOfferBase:: |
public | function |
Gets the conditions. Overrides OrderItemPromotionOfferInterface:: |
|
OrderItemPromotionOfferBase:: |
public | function |
Sets the conditions. Overrides OrderItemPromotionOfferInterface:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PromotionOfferBase:: |
protected | property | The rounder. | |
PromotionOfferBase:: |
protected | function | Asserts that the given entity is of the expected type. | |
PromotionOfferBase:: |
public | function |
Allows an offer to clean up any modifications done to the given entity. Overrides PromotionOfferInterface:: |
1 |
PromotionOfferBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
PromotionOfferBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
PromotionOfferBase:: |
public | function |
Gets the offer entity type ID. Overrides PromotionOfferInterface:: |
|
PromotionOfferBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
PromotionOfferBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
2 |
PromotionOfferBase:: |
public | function |
Constructs a new PromotionOfferBase object. Overrides PluginBase:: |
1 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |