You are here

public function VariationXquantityStockRotate::submitConfigurationForm in Commerce Extended Quantity 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

modules/xquantity_stock/src/Plugin/Action/VariationXquantityStockRotate.php, line 86

Class

VariationXquantityStockRotate
Rotate variation stock.

Namespace

Drupal\xquantity_stock\Plugin\Action

Code

public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getTriggeringElement()['#id'] != 'edit-cancel') {
    if (!($threshold = $form_state
      ->getValue('threshold')) || !is_numeric($threshold)) {
      \Drupal::messenger()
        ->AddError($this
        ->t('The inserted threshold is not numeric.'));
      return;
    }
    $type_manager = \Drupal::entityTypeManager();
    $storage = $type_manager
      ->getStorage('commerce_order');
    $query = $storage
      ->getQuery();
    $query
      ->accessCheck(FALSE);
    $time = time() - $threshold;
    $query
      ->condition('changed', $time, '<');
    $query
      ->condition('cart', '1', '=');
    $query
      ->condition('locked', '0', '=');
    if ($orders = $query
      ->execute()) {
      $cart_manager = \Drupal::service('commerce_cart.cart_manager');
      $storage = $type_manager
        ->getStorage('commerce_order_item');
      foreach ($form_state
        ->get('variations') as $id) {
        $query = $storage
          ->getQuery();
        $query
          ->accessCheck(FALSE);
        $query
          ->condition('order_id', $orders, 'IN');
        $query
          ->condition('purchased_entity', $id, '=');
        if ($order_items = $query
          ->execute()) {
          foreach ($storage
            ->loadMultiple($order_items) as $order_item) {
            $cart_manager
              ->removeOrderItem($order_item
              ->getOrder(), $order_item);
          }
        }
      }
    }
  }
}