class ShipmentPercentageOff in Commerce Shipping 8.2
Provides the percentage off offer for shipments.
Plugin annotation
@CommercePromotionOffer(
id = "shipment_percentage_off",
label = @Translation("Percentage off the shipment amount"),
entity_type = "commerce_order"
)
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_shipping\Plugin\Commerce\PromotionOffer\ShipmentPromotionOfferBase implements ShipmentPromotionOfferInterface
- class \Drupal\commerce_shipping\Plugin\Commerce\PromotionOffer\ShipmentPercentageOff uses PercentageOffTrait
- class \Drupal\commerce_shipping\Plugin\Commerce\PromotionOffer\ShipmentPromotionOfferBase implements ShipmentPromotionOfferInterface
- class \Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer\PromotionOfferBase implements PromotionOfferInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ShipmentPercentageOff
File
- src/
Plugin/ Commerce/ PromotionOffer/ ShipmentPercentageOff.php, line 19
Namespace
Drupal\commerce_shipping\Plugin\Commerce\PromotionOfferView source
class ShipmentPercentageOff extends ShipmentPromotionOfferBase {
use PercentageOffTrait;
/**
* {@inheritdoc}
*/
public function applyToShipment(ShipmentInterface $shipment, PromotionInterface $promotion) {
$percentage = $this
->getPercentage();
// The offer amount is calculated from the unreduced shipment amount.
$amount = $shipment
->getAmount()
->multiply($percentage);
$amount = $this->rounder
->round($amount);
// 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'),
'percentage' => $percentage,
'source_id' => $promotion
->id(),
'included' => $this
->isDisplayInclusive(),
]));
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PercentageOffTrait:: |
public | function | ||
PercentageOffTrait:: |
public | function | ||
PercentageOffTrait:: |
protected | function | Gets the percentage. | |
PercentageOffTrait:: |
public | function | ||
PercentageOffTrait:: |
public | function | ||
PercentageOffTrait:: |
public | function | ||
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 | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
PromotionOfferBase:: |
public | function |
Gets the offer entity type ID. Overrides PromotionOfferInterface:: |
|
ShipmentPercentageOff:: |
public | function |
Applies the offer to the given shipment. Overrides ShipmentPromotionOfferBase:: |
|
ShipmentPromotionOfferBase:: |
protected | property | The entity UUID mapper. | |
ShipmentPromotionOfferBase:: |
protected | property | The shipping order manager. | |
ShipmentPromotionOfferBase:: |
protected | function | Checks whether the promotion can be applied to the given shipment. | |
ShipmentPromotionOfferBase:: |
public | function |
Applies the offer to the given entity. Overrides PromotionOfferInterface:: |
|
ShipmentPromotionOfferBase:: |
public static | function |
Creates an instance of the plugin. Overrides PromotionOfferBase:: |
|
ShipmentPromotionOfferBase:: |
protected | function | Gets the configured shipping method UUIDs. | |
ShipmentPromotionOfferBase:: |
public | function |
Gets whether the offer is display inclusive. Overrides ShipmentPromotionOfferInterface:: |
|
ShipmentPromotionOfferBase:: |
public | function |
Constructs a new ShipmentPromotionOfferBase object. Overrides PromotionOfferBase:: |
|
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. |