You are here

public function VariationPriceAdjust::submitConfigurationForm in Commerce Bulk 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Overrides PluginFormInterface::submitConfigurationForm

File

src/Plugin/Action/VariationPriceAdjust.php, line 88

Class

VariationPriceAdjust
Adjust variation price.

Namespace

Drupal\commerce_bulk\Plugin\Action

Code

public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getTriggeringElement()['#id'] != 'edit-cancel') {
    $values = $form_state
      ->getValues();
    if (!is_numeric($value = $values['adjust_value'])) {
      \Drupal::messenger()
        ->AddError($this
        ->t('The inserted adjust value is not numeric.'));
      return;
    }
    $set = $values['price_type'] == 'list_price' ? 'setListPrice' : 'setPrice';
    $get = $values['price_type'] == 'list_price' ? 'getListPrice' : 'getPrice';
    $op = $values['adjust_op'] == 'add' ? 'add' : 'subtract';
    foreach ($form_state
      ->get('variations') as $variation) {
      if (!($price = $variation
        ->{$get}())) {
        continue;
      }
      if ($values['adjust_type'] == 'fixed_number') {
        $adjust_price = new Price($value, $price
          ->getCurrencyCode());
      }
      else {
        $adjust_price = $price
          ->divide('100')
          ->multiply($value);
      }
      $price = $price
        ->{$op}($adjust_price);
      if ($price
        ->isNegative()) {
        $price = $price
          ->multiply('0');
      }
      $variation
        ->{$set}($price)
        ->save();
    }
  }
}