Commit 0312d4a2 authored by Jonathan Sacksick's avatar Jonathan Sacksick Committed by Jonathan Sacksick
Browse files

Issue #2424487 by jsacksick, MegaChriz, bojanz, zaporylie: Add condition...

Issue #2424487 by jsacksick, MegaChriz, bojanz, zaporylie: Add condition support for commerce_tax_type entity type.
parent 96f7f035
Loading
Loading
Loading
Loading
+7 −0
Changes for modules/tax/config/schema/commerce_tax.schema.yml: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -21,6 +21,13 @@ commerce_tax.commerce_tax_type.*:
      label: 'Plugin'
    configuration:
      type: commerce_tax.commerce_tax_type.plugin.[%parent.plugin]
    conditions:
      type: sequence
      sequence:
        type: commerce_condition
    conditionOperator:
      type: string
      label: 'Condition operator'

commerce_tax.commerce_tax_type.plugin.*:
  type: commerce_tax_type_configuration
+71 −0
Changes for modules/tax/src/Entity/TaxType.php: 71 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -3,6 +3,9 @@
namespace Drupal\commerce_tax\Entity;

use Drupal\commerce\CommerceSinglePluginCollection;
use Drupal\commerce\ConditionGroup;
use Drupal\commerce\Plugin\Commerce\Condition\ParentEntityAwareInterface;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\ConfigEntityInterface;

@@ -45,6 +48,8 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface;
 *     "label",
 *     "plugin",
 *     "configuration",
 *     "conditions",
 *     "conditionOperator",
 *   },
 *   links = {
 *     "add-form" = "/admin/commerce/config/tax-types/add",
@@ -85,6 +90,20 @@ class TaxType extends ConfigEntityBase implements TaxTypeInterface {
   */
  protected $configuration = [];

  /**
   * The conditions.
   *
   * @var array
   */
  protected $conditions = [];

  /**
   * The condition operator.
   *
   * @var string
   */
  protected $conditionOperator = 'AND';

  /**
   * The plugin collection that holds the tax type plugin.
   *
@@ -132,6 +151,58 @@ class TaxType extends ConfigEntityBase implements TaxTypeInterface {
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getConditions() {
    /** @var \Drupal\commerce\ConditionManager $plugin_manager */
    $plugin_manager = \Drupal::service('plugin.manager.commerce_condition');
    $conditions = [];
    foreach ($this->conditions as $condition) {
      if (empty($condition['plugin'])) {
        continue;
      }
      $condition = $plugin_manager->createInstance($condition['plugin'], (array) $condition['configuration']);
      if ($condition instanceof ParentEntityAwareInterface) {
        $condition->setParentEntity($this);
      }
      $conditions[] = $condition;
    }
    return $conditions;
  }

  /**
   * {@inheritdoc}
   */
  public function getConditionOperator() {
    return $this->conditionOperator;
  }

  /**
   * {@inheritdoc}
   */
  public function setConditionOperator($condition_operator) {
    $this->conditionOperator = $condition_operator;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(OrderInterface $order) {
    $conditions = $this->getConditions();
    if (!$conditions) {
      return TRUE;
    }
    $order_conditions = array_filter($conditions, function ($condition) {
      /** @var \Drupal\commerce\Plugin\Commerce\Condition\ConditionInterface $condition */
      return $condition->getEntityTypeId() == 'commerce_order';
    });
    $order_conditions = new ConditionGroup($order_conditions, $this->getConditionOperator());

    return $order_conditions->evaluate($order);
  }

  /**
   * {@inheritdoc}
   */
+40 −0
Changes for modules/tax/src/Entity/TaxTypeInterface.php: 40 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\commerce_tax\Entity;

use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;

@@ -56,4 +57,43 @@ interface TaxTypeInterface extends ConfigEntityInterface, EntityWithPluginCollec
   */
  public function setPluginConfiguration(array $configuration);

  /**
   * Gets the conditions.
   *
   * @return \Drupal\commerce\Plugin\Commerce\Condition\ConditionInterface[]
   *   The conditions.
   */
  public function getConditions();

  /**
   * Gets the tax type condition operator.
   *
   * @return string
   *   The condition operator. Possible values: AND, OR.
   */
  public function getConditionOperator();

  /**
   * Sets the tax type condition operator.
   *
   * @param string $condition_operator
   *   The condition operator.
   *
   * @return $this
   */
  public function setConditionOperator($condition_operator);

  /**
   * Checks whether the tax type applies to the given order.
   *
   * Ensures that the conditions pass.
   *
   * @param \Drupal\commerce_order\Entity\OrderInterface $order
   *   The order.
   *
   * @return bool
   *   TRUE if the tax type applies, FALSE otherwise.
   */
  public function applies(OrderInterface $order);

}
+17 −0
Changes for modules/tax/src/Form/TaxTypeForm.php: 17 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -114,6 +114,23 @@ class TaxTypeForm extends EntityForm {
      '#title' => $this->t('Enabled'),
      '#default_value' => $type->status(),
    ];
    $form['conditions'] = [
      '#type' => 'commerce_conditions',
      '#title' => $this->t('Conditions'),
      '#entity_types' => ['commerce_order'],
      '#parent_entity_type' => 'commerce_tax_type',
      '#default_value' => $type->get('conditions'),
    ];
    $form['conditionOperator'] = [
      '#type' => 'radios',
      '#title' => $this->t('Condition operator'),
      '#title_display' => 'invisible',
      '#options' => [
        'AND' => $this->t('All conditions must pass'),
        'OR' => $this->t('Only one condition must pass'),
      ],
      '#default_value' => $type->getConditionOperator(),
    ];

    return $form;
  }
+3 −0
Changes for modules/tax/src/TaxOrderProcessor.php: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -56,6 +56,9 @@ class TaxOrderProcessor implements OrderProcessorInterface {
  public function process(OrderInterface $order) {
    $tax_types = $this->getTaxTypes();
    foreach ($tax_types as $tax_type) {
      if (!$tax_type->applies($order)) {
        continue;
      }
      if ($tax_type->getPlugin()->applies($order)) {
        $tax_type->getPlugin()->apply($order);
      }
Loading