You are here

public function OrderItemQuantity::evaluate in Commerce Core 8.2

Evaluates the condition.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

Return value

bool TRUE if the condition has been met, FALSE otherwise.

Overrides ConditionInterface::evaluate

File

modules/promotion/src/Plugin/Commerce/Condition/OrderItemQuantity.php, line 81

Class

OrderItemQuantity
Provides the total discounted product quantity condition.

Namespace

Drupal\commerce_promotion\Plugin\Commerce\Condition

Code

public function evaluate(EntityInterface $entity) {
  $this
    ->assertEntity($entity);

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $entity;

  /** @var \Drupal\commerce_promotion\Entity\PromotionInterface $promotion */
  $promotion = $this->parentEntity;
  $offer = $promotion
    ->getOffer();
  $quantity = '0';
  foreach ($order
    ->getItems() as $order_item) {

    // If the offer has conditions, skip order items that don't match.
    if ($offer instanceof OrderItemPromotionOfferInterface) {
      $condition_group = new ConditionGroup($offer
        ->getConditions(), $offer
        ->getConditionOperator());
      if (!$condition_group
        ->evaluate($order_item)) {
        continue;
      }
    }
    $quantity = Calculator::add($quantity, $order_item
      ->getQuantity());
  }
  switch ($this->configuration['operator']) {
    case '>=':
      return $quantity >= $this->configuration['quantity'];
    case '>':
      return $quantity > $this->configuration['quantity'];
    case '<=':
      return $quantity <= $this->configuration['quantity'];
    case '<':
      return $quantity < $this->configuration['quantity'];
    case '==':
      return $quantity == $this->configuration['quantity'];
    default:
      throw new \InvalidArgumentException("Invalid operator {$this->configuration['operator']}");
  }
}