You are here

public function Products::addProductSubmit in Ubercart 8.4

Form submit callback: add a product to an order.

File

uc_order/src/Plugin/Ubercart/OrderPane/Products.php, line 521

Class

Products
Manage the products an order contains.

Namespace

Drupal\uc_order\Plugin\Ubercart\OrderPane

Code

public function addProductSubmit($form, FormStateInterface $form_state) {
  $form_state
    ->set('products_action', 'products_select');
  $form_state
    ->set('refresh_products', TRUE);
  $form_state
    ->setRebuild();
  $order = $form['#order'];
  $data = \Drupal::moduleHandler()
    ->invokeAll('uc_add_to_cart_data', [
    $form_state
      ->getValue('product_controls'),
  ]);
  $values = uc_product_load_variant(intval($form_state
    ->getValue([
    'product_controls',
    'nid',
  ])), $data)
    ->toArray();
  $values['qty'] = $form_state
    ->getValue([
    'product_controls',
    'qty',
  ]);
  $values['order_id'] = $order
    ->id();
  $product = OrderProduct::create($values);
  \Drupal::moduleHandler()
    ->alter('uc_order_product', $product, $order);
  $product
    ->save();
  $order->products[] = $product;
  $order
    ->logChanges([
    $this
      ->t('Added (@qty) @title to order.', [
      '@qty' => $product->qty->value,
      '@title' => $product->title->value,
    ]),
  ]);

  // Decrement stock.
  if (\Drupal::moduleHandler()
    ->moduleExists('uc_stock')) {
    uc_stock_adjust_product_stock($product, 0, $order);
  }

  // Add this product to the form values for accurate tax calculations.
  $products = $form_state
    ->getValue('products');
  $products[] = $product;
  $form_state
    ->setValue('products', $products);
}