You are here

OrderTotalPrice.php in Commerce Core 8.2

File

modules/order/src/Plugin/Commerce/Condition/OrderTotalPrice.php
View source
<?php

namespace Drupal\commerce_order\Plugin\Commerce\Condition;

use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce_price\Price;

/**
 * Provides the total price condition for orders.
 *
 * @CommerceCondition(
 *   id = "order_total_price",
 *   label = @Translation("Total price"),
 *   display_label = @Translation("Current order total"),
 *   category = @Translation("Order", context = "Commerce"),
 *   entity_type = "commerce_order",
 * )
 */
class OrderTotalPrice extends ConditionBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'operator' => '>',
      'amount' => NULL,
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $amount = $this->configuration['amount'];

    // An #ajax bug can cause $amount to be incomplete.
    if (isset($amount) && !isset($amount['number'], $amount['currency_code'])) {
      $amount = NULL;
    }
    $form['operator'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Operator'),
      '#options' => $this
        ->getComparisonOperators(),
      '#default_value' => $this->configuration['operator'],
      '#required' => TRUE,
    ];
    $form['amount'] = [
      '#type' => 'commerce_price',
      '#title' => $this
        ->t('Amount'),
      '#default_value' => $amount,
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    $values = $form_state
      ->getValue($form['#parents']);
    $this->configuration['operator'] = $values['operator'];
    $this->configuration['amount'] = $values['amount'];
  }

  /**
   * {@inheritdoc}
   */
  public function evaluate(EntityInterface $entity) {
    $this
      ->assertEntity($entity);

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $entity;
    $total_price = $order
      ->getTotalPrice();
    if (!$total_price) {
      return FALSE;
    }
    $condition_price = Price::fromArray($this->configuration['amount']);
    if ($total_price
      ->getCurrencyCode() != $condition_price
      ->getCurrencyCode()) {
      return FALSE;
    }
    switch ($this->configuration['operator']) {
      case '>=':
        return $total_price
          ->greaterThanOrEqual($condition_price);
      case '>':
        return $total_price
          ->greaterThan($condition_price);
      case '<=':
        return $total_price
          ->lessThanOrEqual($condition_price);
      case '<':
        return $total_price
          ->lessThan($condition_price);
      case '==':
        return $total_price
          ->equals($condition_price);
      default:
        throw new \InvalidArgumentException("Invalid operator {$this->configuration['operator']}");
    }
  }

}

Classes

Namesort descending Description
OrderTotalPrice Provides the total price condition for orders.