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;
class OrderTotalPrice extends ConditionBase {
public function defaultConfiguration() {
return [
'operator' => '>',
'amount' => NULL,
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$amount = $this->configuration['amount'];
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;
}
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'];
}
public function evaluate(EntityInterface $entity) {
$this
->assertEntity($entity);
$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']}");
}
}
}