OrderFixedAmountOff.php in Commerce Core 8.2
File
modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderFixedAmountOff.php
View source
<?php
namespace Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_promotion\Entity\PromotionInterface;
use Drupal\Core\Entity\EntityInterface;
class OrderFixedAmountOff extends OrderPromotionOfferBase {
use FixedAmountOffTrait;
public function apply(EntityInterface $entity, PromotionInterface $promotion) {
$this
->assertEntity($entity);
$order = $entity;
$subtotal_price = $order
->getSubTotalPrice();
$amount = $this
->getAmount();
if ($subtotal_price
->getCurrencyCode() != $amount
->getCurrencyCode()) {
return;
}
$total_price = $order
->getTotalPrice();
if ($total_price && $amount
->greaterThan($total_price)) {
$amount = $total_price;
}
if ($amount
->isZero()) {
return;
}
$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(),
]));
}
}
}
}