public function OrderFixedAmountOff::apply in Commerce Currency Resolver 8
Applies the offer to the given entity.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The entity.
\Drupal\commerce_promotion\Entity\PromotionInterface $promotion: THe parent promotion.
Overrides OrderFixedAmountOff::apply
File
- src/
Plugin/ Commerce/ PromotionOffer/ OrderFixedAmountOff.php, line 23
Class
- OrderFixedAmountOff
- Provides the fixed amount off offer for orders with multi-currency support.
Namespace
Drupal\commerce_currency_resolver\Plugin\Commerce\PromotionOfferCode
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\OrderInterface $order */
$order = $entity;
$subtotal_price = $order
->getSubTotalPrice();
$amount = $this
->getPrice($this
->getAmount());
// The promotion amount can't be larger than the subtotal, to avoid
// potentially having a negative order total.
if ($amount
->greaterThan($subtotal_price)) {
$amount = $subtotal_price;
}
// Split the amount between order items.
$amounts = $this->splitter
->split($order, $amount);
foreach ($order
->getItems() as $order_item) {
if (isset($amounts[$order_item
->id()])) {
$order_item
->addAdjustment(new Adjustment([
'type' => 'promotion',
'label' => $promotion
->getDisplayName() ?: $this
->t('Discount'),
'amount' => $amounts[$order_item
->id()]
->multiply('-1'),
'source_id' => $promotion
->id(),
]));
}
}
}