You are here

OrderCurrency.php in Commerce Core 8.2

File

modules/order/src/Plugin/Commerce/Condition/OrderCurrency.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;

/**
 * Provides the currency condition for orders.
 *
 * @CommerceCondition(
 *   id = "order_currency",
 *   label = @Translation("Order currency"),
 *   category = @Translation("Order", context = "Commerce"),
 *   entity_type = "commerce_order",
 * )
 */
class OrderCurrency extends ConditionBase {

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

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['currencies'] = [
      '#type' => 'commerce_entity_select',
      '#title' => $this
        ->t('Currencies'),
      '#default_value' => $this->configuration['currencies'],
      '#target_type' => 'commerce_currency',
      '#hide_single_entity' => FALSE,
      '#multiple' => TRUE,
      '#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['currencies'] = $values['currencies'];
  }

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

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $entity;
    $total_price = $order
      ->getTotalPrice();
    $order_currency = $total_price ? $total_price
      ->getCurrencyCode() : '';
    return in_array($order_currency, $this->configuration['currencies']);
  }

}

Classes

Namesort descending Description
OrderCurrency Provides the currency condition for orders.