public function ShipmentFixedAmountOff::applyToShipment in Commerce Shipping 8.2
Applies the offer to the given shipment.
Parameters
\Drupal\commerce_shipping\Entity\ShipmentInterface $shipment: The shipment.
\Drupal\commerce_promotion\Entity\PromotionInterface $promotion: The parent promotion.
Overrides ShipmentPromotionOfferBase::applyToShipment
File
- src/
Plugin/ Commerce/ PromotionOffer/ ShipmentFixedAmountOff.php, line 26
Class
- ShipmentFixedAmountOff
- Provides the fixed amount off offer for shipments.
Namespace
Drupal\commerce_shipping\Plugin\Commerce\PromotionOfferCode
public function applyToShipment(ShipmentInterface $shipment, PromotionInterface $promotion) {
$amount = $this
->getAmount();
if ($amount
->getCurrencyCode() != $shipment
->getAmount()
->getCurrencyCode()) {
return;
}
// 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'),
'source_id' => $promotion
->id(),
'included' => $this
->isDisplayInclusive(),
]));
}