You are here

public function ShipmentPromotionOfferBase::apply in Commerce Shipping 8.2

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 PromotionOfferInterface::apply

File

src/Plugin/Commerce/PromotionOffer/ShipmentPromotionOfferBase.php, line 181

Class

ShipmentPromotionOfferBase
Provides the base class for shipment offers.

Namespace

Drupal\commerce_shipping\Plugin\Commerce\PromotionOffer

Code

public function apply(EntityInterface $order, PromotionInterface $promotion) {
  assert($order instanceof OrderInterface);
  if (!$order
    ->hasField('shipments') || $order
    ->get('shipments')
    ->isEmpty()) {
    return;
  }

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface[] $shipments */
  $shipments = $order
    ->get('shipments')
    ->referencedEntities();
  foreach ($shipments as $shipment) {
    if ($this
      ->appliesToShipment($shipment)) {
      $this
        ->applyToShipment($shipment, $promotion);
    }
  }
}