You are here

public function Promotion::apply in Commerce Core 8.2

Applies the promotion to the given order.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The order.

Overrides PromotionInterface::apply

File

modules/promotion/src/Entity/Promotion.php, line 590

Class

Promotion
Defines the promotion entity class.

Namespace

Drupal\commerce_promotion\Entity

Code

public function apply(OrderInterface $order) {
  $offer = $this
    ->getOffer();
  if ($offer instanceof OrderItemPromotionOfferInterface) {
    $offer_conditions = new ConditionGroup($offer
      ->getConditions(), $offer
      ->getConditionOperator());

    // Apply the offer to order items that pass the conditions.
    foreach ($order
      ->getItems() as $order_item) {

      // Skip order items with a null unit price or with a quantity = 0.
      if (!$order_item
        ->getUnitPrice() || Calculator::compare($order_item
        ->getQuantity(), '0') === 0) {
        continue;
      }
      if ($offer_conditions
        ->evaluate($order_item)) {
        $offer
          ->apply($order_item, $this);
      }
    }
  }
  else {
    $offer
      ->apply($order, $this);
  }
}