You are here

public function Donation::submitPaneForm in Commerce Donate 8

Handles the submission of an pane form.

Parameters

array $pane_form: The pane form.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the parent form.

array $complete_form: The complete form structure.

Overrides CheckoutPaneBase::submitPaneForm

File

src/Plugin/Commerce/CheckoutPane/Donation.php, line 139

Class

Donation
Provides the donation pane.

Namespace

Drupal\commerce_donate\Plugin\Commerce\CheckoutPane

Code

public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
  $order_item = $this
    ->getOrderItem();
  $values = $form_state
    ->getValue($pane_form['#parents']);
  $amount = $values['details']['amount'][0];
  $make_donation = $values['donation'];
  $current_currency = \Drupal::service('commerce_currency_resolver.current_currency');
  $selected_currency = $current_currency
    ->getCurrency();
  $entity_type_manager = \Drupal::service('entity_type.manager');
  $currency = $entity_type_manager
    ->getStorage('commerce_currency')
    ->load($selected_currency);
  $currency_symbol = $currency
    ->getSymbol();
  $currency_formatter = \Drupal::service('commerce_price.currency_formatter');
  $amount_label = $currency_formatter
    ->format($amount, $selected_currency);
  $order_item->title = t('@amount donation', [
    '@amount' => $amount_label,
  ]);
  $order_item->unit_price = [
    'number' => $amount,
    'currency_code' => $selected_currency,
  ];
  $order_item->field_in_memory = $values['details']['in_memory'];
  $order_item->field_in_memory_name = $values['details']['in_memory_name'];
  $order_item->field_in_memory_memorial = $values['details']['in_memory_memorial'];
  $order_item
    ->save();

  // Add or update Donation Line item.
  if (!$this->order
    ->hasItem($order_item) && $make_donation) {
    $this->order
      ->addItem($order_item);
  }

  // Remove Donation if required.
  if (!$make_donation && $this->order
    ->hasItem($order_item)) {
    $this->order
      ->removeItem($order_item);
  }
}