You are here

public function Products::removeProduct in Ubercart 8.4

Form submit callback: remove a product from an order.

File

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

Class

Products
Manage the products an order contains.

Namespace

Drupal\uc_order\Plugin\Ubercart\OrderPane

Code

public function removeProduct($form, FormStateInterface $form_state) {
  $form_state
    ->set('refresh_products', TRUE);

  /** @var \Drupal\uc_order\Entity\Order $order */
  $order = $form['#order'];
  $triggering_element = $form_state
    ->getTriggeringElement();
  $order_product_id = intval($triggering_element['#return_value']);

  /** @var \Drupal\uc_order\Entity\OrderProduct $product */
  $product = $order->products[$order_product_id];
  if (\Drupal::moduleHandler()
    ->moduleExists('uc_stock')) {

    // Replace stock immediately.
    uc_stock_adjust($product->model->value, $product->qty->value);
  }
  $product
    ->delete();
  unset($order->products[$order_product_id]);
  $order
    ->logChanges([
    $this
      ->t('Removed %title from order.', [
      '%title' => $product->title->value,
    ]),
  ]);
}