OrderItemFixedAmountOff.php in Commerce Core 8.2
File
modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderItemFixedAmountOff.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 OrderItemFixedAmountOff extends OrderItemPromotionOfferBase {
use FixedAmountOffTrait;
public function apply(EntityInterface $entity, PromotionInterface $promotion) {
$this
->assertEntity($entity);
$order_item = $entity;
$adjusted_total_price = $order_item
->getAdjustedTotalPrice([
'promotion',
]);
$amount = $this
->getAmount();
if ($adjusted_total_price
->getCurrencyCode() != $amount
->getCurrencyCode()) {
return;
}
if ($this->configuration['display_inclusive']) {
$adjusted_unit_price = $order_item
->getAdjustedUnitPrice([
'promotion',
]);
if ($adjusted_unit_price
->isZero()) {
return;
}
if ($amount
->greaterThan($adjusted_unit_price)) {
$amount = $adjusted_unit_price;
}
$unit_price = $order_item
->getUnitPrice();
$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($adjusted_total_price)) {
$adjustment_amount = $adjusted_total_price;
}
}
if ($adjustment_amount
->isZero()) {
return;
}
$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'],
]));
}
}