commerce-8.x-2.8/modules/order/src/Plugin/Commerce/Condition/OrderTotalPrice.php
modules/order/src/Plugin/Commerce/Condition/OrderTotalPrice.php
<?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"), * 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' => t('Operator'), '#options' => $this->getComparisonOperators(), '#default_value' => $this->configuration['operator'], '#required' => TRUE, ]; $form['amount'] = [ '#type' => 'commerce_price', '#title' => 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(); $condition_price = new Price($this->configuration['amount']['number'], $this->configuration['amount']['currency_code']); 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']}"); } } }