You are here

public function CombinationOffer::apply in Commerce Core 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

modules/promotion/src/Plugin/Commerce/PromotionOffer/CombinationOffer.php, line 237

Class

CombinationOffer
Provides the 'combination_offer' offer plugin.

Namespace

Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer

Code

public function apply(EntityInterface $entity, PromotionInterface $promotion) {
  assert($entity instanceof OrderInterface);

  // This is copied from Promotion::apply().
  foreach ($this
    ->getOffers() as $offer) {
    if ($offer instanceof OrderItemPromotionOfferInterface) {
      $offer_conditions = new ConditionGroup($offer
        ->getConditions(), $offer
        ->getConditionOperator());

      // Apply the offer to order items that pass the conditions.
      foreach ($entity
        ->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, $promotion);
        }
      }
    }
    else {
      $offer
        ->apply($entity, $promotion);
    }
  }
}