You are here

public function Promotion::applies in Commerce Core 8.2

Checks whether the promotion can be applied to the given order.

Ensures that the promotion is compatible with other promotions on the order, and that the conditions pass.

Parameters

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

Return value

bool TRUE if promotion can be applied, FALSE otherwise.

Overrides PromotionInterface::applies

File

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

Class

Promotion
Defines the promotion entity class.

Namespace

Drupal\commerce_promotion\Entity

Code

public function applies(OrderInterface $order) {

  // Check compatibility.
  // @todo port remaining strategies from Commerce Discount #2762997.
  switch ($this
    ->getCompatibility()) {
    case self::COMPATIBLE_NONE:

      // If there are any existing promotions, then this cannot apply.
      foreach ($order
        ->collectAdjustments() as $adjustment) {
        if ($adjustment
          ->getType() == 'promotion') {
          return FALSE;
        }
      }
      break;
    case self::COMPATIBLE_ANY:
      break;
  }
  $conditions = $this
    ->getConditions();
  if (!$conditions) {

    // Promotions without conditions always apply.
    return TRUE;
  }

  // Filter the conditions just in case there are leftover order item
  // conditions (which have been moved to offer conditions).
  $conditions = array_filter($conditions, function ($condition) {

    /** @var \Drupal\commerce\Plugin\Commerce\Condition\ConditionInterface $condition */
    return $condition
      ->getEntityTypeId() == 'commerce_order';
  });
  $condition_group = new ConditionGroup($conditions, $this
    ->getConditionOperator());
  return $condition_group
    ->evaluate($order);
}