|
<?php |
|
|
|
namespace Drupal\my_custom_commerce_range_condition\Plugin\Commerce\Condition; |
|
|
|
use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase; |
|
use Drupal\commerce_price\Price; |
|
use Drupal\Core\Entity\EntityInterface; |
|
use Drupal\Core\Form\FormStateInterface; |
|
|
|
/** |
|
* Provides the total price range condition for orders. |
|
* |
|
* To be used when willing to trigger condition when the order total falls |
|
* within a range of price (including adjustments) |
|
* e.g. $100 => order total <= $200. |
|
* |
|
* @CommerceCondition( |
|
* id="order_total_price_range", |
|
* label=@Translation("Total price range"), |
|
* display_label=@Translation("Current order total within a range"), |
|
* category=@Translation("Order", context="Commerce"), |
|
* entity_type="commerce_order", |
|
* ) |
|
*/ |
|
class OrderTotalPriceRange extends ConditionBase { |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public function defaultConfiguration() { |
|
return [ |
|
'amount_from' => NULL, |
|
'amount_to' => NULL, |
|
] + parent::defaultConfiguration(); |
|
} |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
|
$form = parent::buildConfigurationForm($form, $form_state); |
|
|
|
$amount_from = $this->configuration['amount_from']; |
|
$amount_to = $this->configuration['amount_to']; |
|
|
|
// An #ajax bug can cause $amount_from to be incomplete. |
|
if (isset($amount_from) && !isset($amount_from['number'], $amount_from['currency_code'])) { |
|
$amount_from = NULL; |
|
} |
|
|
|
// An #ajax bug can cause $amount_to to be incomplete. |
|
if (isset($amount_to) && !isset($amount_to['number'], $amount_to['currency_code'])) { |
|
$amount_to = NULL; |
|
} |
|
|
|
$form['amount_from'] = [ |
|
'#type' => 'commerce_price', |
|
'#title' => $this->t('Amount from'), |
|
'#default_value' => $amount_from, |
|
'#required' => TRUE, |
|
]; |
|
|
|
$form['amount_to'] = [ |
|
'#type' => 'commerce_price', |
|
'#title' => $this->t('Amount to'), |
|
'#default_value' => $amount_to, |
|
'#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['amount_from'] = $values['amount_from']; |
|
$this->configuration['amount_to'] = $values['amount_to']; |
|
} |
|
|
|
/** |
|
* {@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_from = Price::fromArray($this->configuration['amount_from']); |
|
|
|
if ($total_price->getCurrencyCode() !== $condition_price_from->getCurrencyCode()) { |
|
return FALSE; |
|
} |
|
|
|
$condition_price_to = Price::fromArray($this->configuration['amount_to']); |
|
|
|
if ($total_price->getCurrencyCode() !== $condition_price_to->getCurrencyCode()) { |
|
return FALSE; |
|
} |
|
|
|
return $total_price->greaterThanOrEqual($condition_price_from) |
|
&& $total_price->lessThanOrEqual($condition_price_to); |
|
} |
|
|
|
} |